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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@skullx/nano-cache

v2.0.1

Published

A lightweight, zero-dependency in-memory key-value cache with configurable TTL and eviction policies.

Readme

@skullx/nano-cache

A lightweight, zero-dependency, in-memory key-value cache for Node.js with configurable TTL expiration, capacity limits, and multiple eviction policies.

Features

  • Zero runtime dependencies
  • Configurable time-to-live (TTL)
  • Custom TTL for individual entries
  • Maximum cache capacity
  • Six eviction policies: LRU, LFU, FIFO, LIFO, MRU, and Random
  • Cache statistics and hit-rate tracking
  • TypeScript declarations included
  • Injectable clock for deterministic testing
  • Built on JavaScript Map

Installation

npm install @skullx/nano-cache

Quick Start

const createCache = require('@skullx/nano-cache');

const cache = createCache();

cache.set('username', 'ujjwal');

console.log(cache.get('username')); // 'ujjwal'
console.log(cache.has('username')); // true
console.log(cache.size());          // 1

Configuration

Pass an options object to createCache().

| Option | Default | Description | |---|---|---| | defaultTtl | 60000 | Default expiration time in milliseconds | | maxEntries | Infinity | Maximum number of entries | | evictionPolicy | 'lru' | Policy used when capacity is reached | | clock | Date.now | Injectable clock function |

Example:

const cache = createCache({
  defaultTtl: 30000,
  maxEntries: 1000,
  evictionPolicy: 'lru',
});

TTL Expiration

TTL is specified in milliseconds.

const cache = createCache({
  defaultTtl: 5000,
});

cache.set('session', 'active');
cache.set('temporary', 'value', 1000);
cache.set('permanent', 'value', Infinity);
  • Positive TTL values expire after the specified duration.
  • 0 means the entry expires immediately.
  • Infinity means the entry never expires.
  • Negative TTL values are rejected.

Expired entries are cleaned up lazily during cache operations such as get(), has(), size(), and before capacity eviction.

Eviction Policies

When maxEntries is reached, the configured policy determines which entry is removed.

| Policy | Behavior | |---|---| | lru | Least Recently Used | | lfu | Least Frequently Used | | fifo | First In, First Out | | lifo | Last In, First Out | | mru | Most Recently Used | | random | Randomly selected entry |

Example:

const cache = createCache({
  maxEntries: 3,
  evictionPolicy: 'lfu',
});

cache.set('a', 1);
cache.set('b', 2);
cache.set('c', 3);
cache.set('d', 4);

The LFU policy evicts the entry with the lowest access frequency. Ties are resolved using recency.

API Reference

createCache(options?)

Creates and returns a cache instance.

set(key, value, customTtl?)

Stores a value under a key. Keys must be non-empty, non-whitespace strings. An optional custom TTL overrides the default TTL.

get(key)

Returns the stored value, or null if the key is missing or expired. A stored null looks the same as a miss, so use has() if you store null values.

has(key)

Returns whether a non-expired entry exists.

del(key)

Deletes an entry. Returns true if an entry was deleted, otherwise false.

clear()

Removes all entries from the cache.

size()

Returns the number of non-expired entries.

stats()

Returns a snapshot of cache statistics.

console.log(cache.stats());

// {
//   hits: 10,
//   misses: 2,
//   evictions: 1,
//   size: 5,
//   hitRate: 0.8333333333333334
// }

resetStats()

Resets hit, miss, and eviction counters without clearing cached entries.

TypeScript

TypeScript declarations are included.

import createCache = require('@skullx/nano-cache');

const cache = createCache<string>({
  maxEntries: 100,
  defaultTtl: 5000,
  evictionPolicy: 'lru',
});

cache.set('name', 'nano-cache');

const value: string | null = cache.get('name');

Supported eviction policy names are available through the EvictionPolicy type in the declaration namespace.

Development

Run the test suite:

npm test

Run TypeScript declaration checks:

npm run test:types

Run benchmarks:

npm run benchmark

Benchmarks

Benchmarks compare native JavaScript Map operations with cache operations and measure the performance of the available eviction policies.

Results depend on the Node.js version, hardware, and workload. Run benchmarks on your own environment before drawing performance conclusions.

License

MIT