Memoizerific

1.11.3 · active · verified Sun Apr 19

Memoizerific is a fast, small, and efficient JavaScript memoization library designed to cache function results, preventing re-execution for identical arguments. Currently at version 1.11.3, it receives regular minor updates, with recent changes focusing on package configuration and documentation. A key differentiator is its use of JavaScript's native `Map()` object (with a performant polyfill where unavailable) for instant lookups, avoiding costly serialization or string manipulation common in other memoization approaches. It supports multiple complex arguments and incorporates Least-Recently-Used (LRU) caching, allowing developers to specify a limit on the number of results stored. The library is built for compatibility across both browser and Node.js environments and is particularly useful in scenarios like Redux for deriving data on the fly. Its small footprint (1kb min/gzip) makes it a lightweight choice.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize Memoizerific with an LRU cache limit and memoize a function. It shows cache hits for identical primitive and complex arguments (same object instance).

import memoizerific from 'memoizerific';

// memoize the 50 most recent argument combinations of our function
const memoized = memoizerific(50)(function(arg1, arg2, arg3) {
    // Simulate a long expensive call
    console.log(`Processing with args: ${arg1}, ${arg2}, ${arg3}`);
    let sum = 0;
    for(let i=0; i<1000000; i++) sum += i; // busy loop
    return `Result for ${arg1}, ${arg2}, ${arg3}: ${sum}`;
});

console.log(memoized(1, 2, 3)); // that took long to process
console.log(memoized(1, 2, 3)); // this one was instant!

const 
    complexArg1 = { a: { b: { c: 99 }}}, 
    complexArg2 = [{ z: 1}, { q: [{ x: 3 }]}],
    complexArg3 = new Set(); 

console.log(memoized(complexArg1, complexArg2, complexArg3)); // slow
console.log(memoized(complexArg1, complexArg2, complexArg3)); // instant!

view raw JSON →