Have ideas to improve npm?Join in the discussion! »

@slimio/timemapTypeScript icon, indicating that this package has built-in type declarations

0.3.2 • Public • Published


version Maintenance MIT dep size Known Vulnerabilities Build Status

ECMAScript 6 Map-Like implementation with keys that have a defined timelife.


Getting Started

This package is available in the Node Package Repository and can be easily installed with npm or yarn.

$ npm i @slimio/timemap
# or 
$ yarn add @slimio/timemap

Usage example

const { strictEqual } = require("assert");
const TimeMap = require("@slimio/timemap");
const col = new TimeMap(1000);
col.on("expiration", (key, value) => {
    console.log(`col key ${key} has expired!`);
col.set("foo", "bar");
col.set("test", true);
strictEqual(col.has("foo"), true);
setTimeout(() => {
    col.set("hello", "world!");
    strictEqual(col.has("foo"), true);
}, 500);
setTimeout(() => {
    strictEqual(col.has("foo"), false);
    strictEqual(col.has("test"), false);
    strictEqual(col.has("hello"), true);
}, 1100);


TimeMap class is extended by a Node.js EventEmitter. The class can trigger several events:

event name description
expiration Expired key are emitted before deletion
const map = new TimeMap(100);
map.on("expiration", (key, value) => {
    console.log(`key: ${key}, value: ${value}`);
map.set("foo", "bar");


Following methods are members of TimeMap class. The type TimeMap.key is declared as follow:

type key = symbol | string | number;
constructor(timeLifeMs: number)

Create a new TimeMap Object. Take an argument which is the time that a key stay alive within the class.

const map = new TimeMap(5000);
map.set("foo", "bar"); // foo will live for the next 5,000 milliseconds

The default timeLifeMs is equal to the value of static member TimeMap.DEFAULT_TIMELIFE_MS (equal to 1000 by default).

const { strictEqual } = require("assert");
const map = new TimeMap();
strictEqual(map.timeLife, TimeMap.DEFAULT_TIMELIFE_MS);
set(key: TimeMap.key, value: any): void

Set a new key in the Collection. Inner timer will be initialized by the first key. The key must be a string or a symbol (no other primitive are accepted).

const { strictEqual } = require("assert");
const map = new TimeMap();
const sym = Symbol("foo");
map.set(sym, "bar");
strictEqual(map.get(sym), "foo");
has(key: TimeMap.key): boolean

Similar to Map.has method. Return true if the key exist within.

const { strictEqual } = require("assert");
const map = new TimeMap(100);
map.set("foo", "bar");
strictEqual(map.has("foo"), true);
setTimeout(() => {
    strictEqual(map.has("foo"), false);
}, 105);
delete(key: TimeMap.key): void

Delete a given key from TimeMap. The key must be a string or a symbol.

const { strictEqual } = require("assert");
const map = new TimeMap(100);
map.once("expiration", (key) => {
    strictEqual(key, "hello");
map.set("foo", "bar");
map.set("hello", "world");
setTimeout(() => {
}, 50)
get< T >(key: TimeMap.key, refreshTimestamp?: boolean): T

Get a given key from the Class. Throw an Error if the key doesn't exist in the Collection (use .has() before).

const assert = require("assert");
const map = new TimeMap(100);
map.set("foo", "bar");
assert.strictEqual(map.get("foo"), "bar");
assert.throws(() => {
}, { name: "Error" });
clear(): void

Clear internal timer and internal data. Everything will be reset.

keys(): IterableIterator< TimeMap.key >

The keys() method returns a new Iterator object that contains the keys for each element in the TimeMap object in insertion order.

const { deepEqual } = require("assert");
const map = new TimeMap();
map.set("foo", "bar");
map.set("yo", "boo");
deepEqual(["foo", "yo"], [...map.keys()]);


All following properties are readonly


The size accessor property returns the number of elements in the TimeMap.

const { strictEqual } = require("assert");
const map = new TimeMap();
map.set("foo", "bar");
strictEqual(map.size, 1);

The timeLife accessor property return the configured time life for keys

const { strictEqual } = require("assert");
const map = new TimeMap(2000);
strictEqual(map.timeLife, 2000);


This project have no dependencies.




npm i @slimio/timemap

DownloadsWeekly Downloads






Unpacked Size

16.6 kB

Total Files


Last publish


  • avatar
  • avatar
  • avatar
  • avatar