Class: TimedMap

TimedMap(ttl)

A Map extension that automatically deletes entries after a specified time-to-live (TTL).

Constructor

new TimedMap(ttl)

Creates a new TimedMap instance.
Parameters:
Name Type Description
ttl number Time-to-live in milliseconds for each key-value pair
Source:
Examples
const cache = new TimedMap(5000); // 5 second TTL
cache.set('key1', 'value1');
console.log(cache.get('key1')); // 'value1'
// After 5 seconds:
console.log(cache.get('key1')); // undefined
// Resetting TTL on value update
const cache = new TimedMap(2000);
cache.set('user', { name: 'Alice' });
// 1 second later:
cache.set('user', { name: 'Alice', age: 30 }); // Resets the 2-second timer

Extends

  • Map

Methods

delete(key) → {boolean}

Deletes a key-value pair and its associated timer.
Parameters:
Name Type Description
key * The key to delete
Source:
Returns:
True if the element was deleted, false if it didn't exist
Type
boolean
Example
const cache = new TimedMap(5000);
cache.set('temp', 'data');
cache.delete('temp'); // Manually delete before TTL expires

set(key, value) → {this}

Sets a value in the map with an automatic deletion timer. If the key already exists, its timer is reset.
Parameters:
Name Type Description
key * The key to set
value * The value to store
Source:
Returns:
The TimedMap instance for chaining
Type
this
Example
const cache = new TimedMap(10000);
cache.set('apiKey', 'xyz123')
     .set('timestamp', Date.now());