Batch Operations
Batch operations allow you to work with multiple storage items at once, which is more efficient than calling individual methods in a loop.
batchSet()
Section titled “batchSet()”Stores multiple items efficiently in a single operation. Each item can have its own expiration time.
Syntax
Section titled “Syntax”await storage.batchSet(items)Parameters
Section titled “Parameters”items(Array) — An array of objects, each with these properties:key(String, required) — The key to storevalue(Any JSON-serializable, required) — The value to storeexpiresIn(Number, optional) — Expiration time in seconds
Returns
Section titled “Returns”Promise<void>
Example
Section titled “Example”await storage.batchSet([ { key: 'user', value: { name: 'Alice' } }, { key: 'theme', value: 'dark' }, { key: 'session', value: 'xyz789', expiresIn: 3600 }, // Expires in 1 hour { key: 'temp', value: 'test', expiresIn: 60 } // Expires in 1 minute]);Use Cases
Section titled “Use Cases”- Initializing multiple storage items when a user logs in
- Saving multiple form fields at once
- Caching multiple API responses with different expiration times
- Restoring state from a backup
batchGet()
Section titled “batchGet()”Retrieves multiple items in a single operation.
Syntax
Section titled “Syntax”await storage.batchGet(keys)Parameters
Section titled “Parameters”keys(Array of Strings) — An array of key names to retrieve
Returns
Section titled “Returns”Promise<Object> — An object mapping requested keys to their values. If a key doesn’t exist or has expired, its value will be null.
Example
Section titled “Example”// Store some itemsawait storage.set('user', { name: 'Alice' });await storage.set('theme', 'dark');await storage.set('lang', 'en');
// Retrieve multiple itemsconst data = await storage.batchGet(['user', 'theme', 'lang', 'missing']);
console.log(data);// Output: {// user: { name: 'Alice' },// theme: 'dark',// lang: 'en',// missing: null// }Use Cases
Section titled “Use Cases”- Loading multiple settings or preferences at once
- Retrieving all data needed for a page in one operation
- Checking multiple cache entries simultaneously
- Bulk data export
batchRemove()
Section titled “batchRemove()”Removes multiple items in a single operation.
Syntax
Section titled “Syntax”await storage.batchRemove(keys)Parameters
Section titled “Parameters”keys(Array of Strings) — An array of key names to remove
Returns
Section titled “Returns”Promise<void>
Example
Section titled “Example”// Store some itemsawait storage.set('temp1', 'value1');await storage.set('temp2', 'value2');await storage.set('temp3', 'value3');await storage.set('keep', 'important');
// Remove multiple items at onceawait storage.batchRemove(['temp1', 'temp2', 'temp3']);
// Verifyconst keys = await storage.keys();console.log(keys); // Output: ['keep']Use Cases
Section titled “Use Cases”- Cleaning up temporary data
- Logging out a user (removing session, token, user data, etc.)
- Clearing cache entries
- Bulk deletion of related items
Complete Batch Example
Section titled “Complete Batch Example”Here’s a comprehensive example using all batch operations together:
// Initialize storage with multiple itemsawait storage.batchSet([ { key: 'user', value: { id: 1, name: 'Alice' } }, { key: 'session', value: 'token-xyz', expiresIn: 3600 }, { key: 'preferences', value: { theme: 'dark', lang: 'en' } }, { key: 'cache:posts', value: [...], expiresIn: 1800 }, { key: 'cache:users', value: [...], expiresIn: 1800 }]);
// Later: Load multiple items at onceconst data = await storage.batchGet([ 'user', 'session', 'preferences', 'cache:posts']);
if (data.user && data.session) { console.log(`Welcome back, ${data.user.name}!`); console.log('Theme:', data.preferences.theme);}
// On logout: Remove all user-related dataawait storage.batchRemove([ 'user', 'session', 'preferences', 'cache:posts', 'cache:users']);Performance Comparison
Section titled “Performance Comparison”Here’s a comparison of individual vs. batch operations:
// ❌ Inefficient: Individual operationsconsole.time('individual');for (let i = 0; i < 100; i++) { await storage.set(`item${i}`, { value: i });}console.timeEnd('individual'); // ~500ms
// ✅ Efficient: Batch operationconsole.time('batch');const items = Array.from({ length: 100 }, (_, i) => ({ key: `item${i}`, value: { value: i }}));await storage.batchSet(items);console.timeEnd('batch'); // ~50msNext Steps
Section titled “Next Steps”Learn about Event Listeners to monitor changes across tabs and windows.