
Security News
High Salaries No Longer Enough to Attract Top Cybersecurity Talent
A survey of 500 cybersecurity pros reveals high pay isn't enough—lack of growth and flexibility is driving attrition and risking organizational security.
map-age-cleaner
Advanced tools
The map-age-cleaner npm package is designed to automatically remove expired items from a Map object in JavaScript based on a specified age. This is particularly useful for managing caches or temporary data storage where items should not persist indefinitely. The package works by wrapping a standard Map object and allowing the developer to specify a maximum age for the items. Once an item exceeds this age, it is automatically removed from the Map, helping to manage memory and data relevance efficiently.
Automatic cleanup of expired items
This feature allows for the automatic removal of items from a Map after they have been in the Map for a specified duration. The `maxAge` option is used to define how long (in milliseconds) an item should remain in the Map before being automatically removed.
const mapAgeCleaner = require('map-age-cleaner');
const myMap = new Map();
mapAgeCleaner(myMap, { maxAge: 10000 });
// Adds an item to the map
myMap.set('key', 'value');
// After 10 seconds, the item is automatically removed
lru-cache is a cache object that deletes the least-recently-used items. It offers similar functionality in terms of managing temporary data with automatic removal, but it bases removal on usage frequency rather than a fixed time duration. This makes it suitable for different scenarios where the access pattern rather than the age of the data determines its relevance.
node-cache is a simple caching module with set, get and delete methods and it supports TTL (time to live) for entries. It provides functionality similar to map-age-cleaner by allowing entries to expire after a certain period. However, node-cache is a more comprehensive caching solution with additional features such as statistics about hits, misses, and keys stored.
Automatically cleanup expired items in a Map
$ npm install map-age-cleaner
import mapAgeCleaner = require('map-age-cleaner');
const map = new Map([
['unicorn', {data: '🦄', maxAge: Date.now() + 1000}]
]);
mapAgeCleaner(map);
map.has('unicorn');
//=> true
// Wait for 1 second...
map.has('unicorn');
//=> false
Note: Items have to be ordered ascending based on the expiry property. This means that the item which will be expired first, should be in the first position of the
Map
.
Returns the Map
instance.
Type: Map
Map instance which should be cleaned up.
Type: string
Default: maxAge
Name of the property which olds the expiry timestamp.
Map
implementation with expirable itemsSet
implementation with expirable keysMIT © Sam Verschueren
FAQs
Automatically cleanup expired items in a Map
The npm package map-age-cleaner receives a total of 3,013,519 weekly downloads. As such, map-age-cleaner popularity was classified as popular.
We found that map-age-cleaner demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
A survey of 500 cybersecurity pros reveals high pay isn't enough—lack of growth and flexibility is driving attrition and risking organizational security.
Product
Socket, the leader in open source security, is now available on Google Cloud Marketplace for simplified procurement and enhanced protection against supply chain attacks.
Security News
Corepack will be phased out from future Node.js releases following a TSC vote.