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

timed-memoize

v2.4.2

Published

Timed memoize

Downloads

2,112

Readme

timed-memoize

Timed memoize.

npm downloads Build Status

Basic usage

import memoize from 'timed-memoize';

// Memoize function return values
const memoizedFunction = memoize(myFunction [, options]);
memoizedFunction('foo'); // returns myFucntion('foo'), saves the value in memory for later use

// Memoize using key/value pairs
const memory = memoize([options]);
memory(key, value); // sets 'key' to 'value'
memory(key); // gets the value

Tiny library that wraps a function to memoize its return values when given specific arguments. Return values are cached on a per-arguments basis. This means that if you call the function with different arguments, then the cache will be missed and a new value will be computed with those arguments (see examples below). Optionally you can specify how to resolve the arguments to a key that is used internally to cache return values.

It is also possible to memoize key/value pairs using a cache.

Works in browsers and Node.js

Options

Memoizing functions

  • fn (required): Function whose return values to memoize.
  • options (default: {}): An object with the following options:
    • timeout (default: 0): The amount of time in milliseconds to keep the function's return value in cache for. Keeps values indefinitely for timeout = -1.
    • hot (default: true): If enabled, keeps track of when the last call to the function was made (with the arguments supplied); it then makes sure the cache is kept for an additional timeout amount of time. This helps keeping the cache alive for frequently used values.
    • resolver (default: args => args): A function that accepts a single array and transforms it into a key. The returned key can be anything that can be used as a key in standard JavaScript objects. Not used when one = true.
    • discardUndefined (default: false): If the underlying function returns undefined, then don't cache the value and re-evaluate the function on the next call.
    • one (default: false) Only ever remember one value. When the memoized function is repeatedly called with the same arguments, the cache will hit and the underlying function is not called. When the memoized function is called with different arguments, the cache will miss and a new value is cached by calling the underlying function. Note: the arguments are checked shallowly for equality.

Returns a function that can be invoked like the underlying function, but returns cached results.

Memoizing key/value pairs

  • options (default: {}): An object with the following options:
    • timeout (default: 0): The amount of time in milliseconds to keep the key/value pair in cache for. Keeps values indefinitely for timeout = -1.
    • hot (default: true): If enabled, keeps track of when the last request for the value was made; it then makes sure the cache is kept for an additional timeout amount of time. This helps keeping the cache alive for frequently used values.

Returns a function that can be used to set key/value pairs, and can return the cached values.

const memory = memoize(options);

is equivalent to

const memory = memoize((x, y) => y, {...options, resolver: args => args[0], discardUndefined: true});

Examples

import memoize from 'timed-memoize';

function myHeavyComputation(x) {
    console.log(`Computing the square root of ${x}...`)
    return Math.sqrt(x * x);
}

// Caches immediately, even when called synchronously.
const myHeavyComputationMemoized = memoize(myHeavyComputation, {timeout: 500, hot: false});
console.log(myHeavyComputationMemoized(1)); // 1, cache miss
console.log(myHeavyComputationMemoized(4)); // 2, cache miss
console.log(myHeavyComputationMemoized(1)); // 1, cache hit

// Cold cache.
// Any values returned by the underlying function are forgotten after 500ms.
setTimeout(() => console.log(myHeavyComputationMemoized(9)), 0); // 3, cache miss
setTimeout(() => console.log(myHeavyComputationMemoized(9)), 300); // 3, cache hit
setTimeout(() => console.log(myHeavyComputationMemoized(9)), 600); // 3, cache miss

// Hot cache.
// Any values returned by the underlying function are forgotten after 500ms.
// If the function is called again with the same arguments, the timer is reset. 
const myHotAndHeavyComputation = memoize(myHeavyComputation, {timeout: 500, hot: true});
setTimeout(() => console.log(myHotAndHeavyComputation(16)), 0); // 4, cache miss
setTimeout(() => console.log(myHotAndHeavyComputation(16)), 300); // 4, cache hit
setTimeout(() => console.log(myHotAndHeavyComputation(16)), 600); // 4, cache hit
setTimeout(() => console.log(myHotAndHeavyComputation(16)), 1200); // 4, cache miss

// Cache with custom resolver.
// Resolver is used to remember return values from past calls.
function mySideEffect(x, y) {
    console.log(y);
    return x + 1;
}
const mySideEffectWithUnimportantArguments = memoize(mySideEffect, {resolver: args => args[0]});
console.log(mySideEffectWithUnimportantArguments(1, 'foo')); // 2, cache miss
console.log(mySideEffectWithUnimportantArguments(1, 'bar')); // 2, cache hit

// Storing and retrieving key/value pairs
const memory = memoize({timeout: 50, hot: false});
memory('foo', 'bar');
console.log(memory('foo')); // bar
setTimeout(() => console.log(memory('foo')), 25); // bar
setTimeout(() => console.log(memory('foo')), 75); // undefined

// Set timeout to -1, keep this value for ever.
const memoizeForever = memoize(myHeavyComputation, {timeout: -1});
setTimeout(() => console.log(memoizeForever(16)), 0); // 4, cache miss
setTimeout(() => console.log(memoizeForever(16)), 500); // 4, cache hit
setTimeout(() => console.log(memoizeForever(16)), 500000000000); // 4, cache hit

// Only ever remember one value, change value based on function arguments.
const memoizeOne = memoize(myHeavyComputation, {one: true});
memoizeOne(16); // 4, cache miss
memoizeOne(16); // 4, cache hit
memoizeOne(16); // 4, cache hit
memoizeOne(25); // 5, cache miss
memoizeOne(25); // 5, cache hit
memoizeOne(16); // 4, cache miss
memoizeOne(16); // 4, cache hit

Installation

Node

npm install --save timed-memoize