npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

expiring-types

v1.0.4

Published

Expiring map and expiring sets

Downloads

14

Readme

expiring-types

A lightweight TypeScript library providing data structures with automatic expiration functionality.

Installation

npm install expiring-types

Features

  • ExpiringMap - A Map-like data structure where entries expire after a specified time
  • ExpiringSet - A Set-like data structure where elements expire after a specified time
  • Fully typed with TypeScript
  • No dependencies
  • Implements standard collection methods and iterators

Usage

ExpiringMap

import { ExpiringMap } from 'expiring-types';

// Create a map with entries that expire after 5000ms (5 seconds)
const cache = new ExpiringMap<string, any>(5000);

// Add items to the map
cache.set('user1', { name: 'John', age: 30 });
cache.set('user2', { name: 'Jane', age: 25 });

// Get an item
const user = cache.get('user1'); // Returns the user object

// Check if an item exists
if (cache.has('user1')) {
  console.log('User exists in cache');
}

// After 5 seconds, the entries will be automatically removed
setTimeout(() => {
  console.log(cache.has('user1')); // false
  console.log(cache.size); // 0
}, 6000);

// Manually delete an entry
cache.delete('user1');

// Clear all entries
cache.clear();

ExpiringSet

import { ExpiringSet } from 'expiring-types';

// Create a set with elements that expire after 10000ms (10 seconds)
const recentUsers = new ExpiringSet<string>(10000);

// Add items to the set
recentUsers.add('user123');
recentUsers.add('user456');

// Check if an element exists
if (recentUsers.has('user123')) {
  console.log('User recently active');
}

// After 10 seconds, the elements will be automatically removed
setTimeout(() => {
  console.log(recentUsers.has('user123')); // false
  console.log(recentUsers.size); // 0
}, 11000);

// Iterate over the set
for (const user of recentUsers) {
  console.log(user);
}

// Manually delete an element
recentUsers.delete('user123');

// Clear all elements
recentUsers.clear();

API Reference

ExpiringMap<K, V>

A Map-like collection where each entry expires after a specified time.

Constructor

  • constructor(ttl: number) - Creates a new ExpiringMap with the specified time-to-live in milliseconds

Methods

  • set(key: K, value: V): this - Adds or updates an entry
  • get(key: K): V | undefined - Retrieves an entry's value
  • has(key: K): boolean - Checks if an entry exists
  • delete(key: K): boolean - Removes an entry
  • clear(): void - Removes all entries
  • keys(): IterableIterator<K> - Returns an iterator of all keys
  • values(): IterableIterator<V> - Returns an iterator of all values
  • entries(): IterableIterator<[K, V]> - Returns an iterator of all [key, value] pairs

Properties

  • size: number - The number of entries in the map

ExpiringSet

A Set-like collection where each element expires after a specified time.

Constructor

  • constructor(ttl: number) - Creates a new ExpiringSet with the specified time-to-live in milliseconds

Methods

  • add(value: T): this - Adds an element
  • has(value: T): boolean - Checks if an element exists
  • delete(value: T): boolean - Removes an element
  • clear(): void - Removes all elements
  • values(): IterableIterator<T> - Returns an iterator of all values
  • keys(): IterableIterator<T> - Returns an iterator of all values
  • entries(): IterableIterator<[T, T]> - Returns an iterator of all [value, value] pairs

Properties

  • size: number - The number of elements in the set

License

MIT