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

fn-memoizer

v1.0.0

Published

A function memoizer with a LRU cache

Readme

fn-memoizer

npm bundle size npm

A function memoizer with a LRU cache

Read more about the module at dev.to post

Install

  npm i fn-memoizer

Usage

Call memoizer with a function to memoize and optional params. All input params are JSON.strigified to create cache key.

supported options:

{
  cacheSize : 10, // defaults to 25
  expiresAt : 1000 // value in ms, defaults to null (no expiry)
}
const memoizer = require("fn-memoizer");

let count = 0;
function calc(values, multiplier, labels) {
  count++;
  const total = values.reduce((acc, x) => x + acc, 0) * multiplier;
  return `${labels.text} => ${total}`;
}
const memoizedCalc = memoizer(calc);

memoizedCalc([10, 2], 2, { text: "A" });
// count = 1

memoizedCalc([1], 1, { text: "B" });
// count = 2

memoizedCalc([10, 2], 2, { text: "A" });
// count = 2, returned cached result

Also supports asynchronous functions which return a promise.

let count = 0;
async function getTodo(id) {
  count++;
  return fetch(`https://jsonplaceholder.typicode.com/todos/${id}`).then((res) =>
    res.json()
  );
}
const memoizedGetTodo = memoizer(getTodo);

await memoizedGetTodo(1);
// count = 1

await memoizedGetTodo(2);
// count = 2

await memoizedGetTodo(1);
// count = 2

Pass cache size, also allows manual clear

let count = 0;
function add(a, b, c = 0) {
  count++;
  return a + b + c;
}
const memoAdd = memoizer(add, { cacheSize: 3 });

  memoAdd(5, 3);
  memoAdd(3, 3);
  memoAdd(1, 2);
  memoAdd(2, 4));
  // count 4
  memoAdd(1, 2);
  // count 4
  memoAdd(5, 3);
  // count 5
  // cache size is 3, hence the least used was removed
  memoAdd.clearCache();
  memoAdd(1, 2);
  // count 6
  // cache was cleared, resulting in source function getting called again

Supports expiry of cached values, accepts expiresAt value in ms

(async function () {
  let count = 0;
  function add(a, b, c = 0) {
    count++;
    return a + b + c;
  }
  const memoAdd = memoizer(add, { cacheSize: 3, expiresAt: 1000 });

  memoAdd(5, 3);
  memoAdd(3, 3);
  memoAdd(1, 2);
  memoAdd(2, 4);
  console.log(count); // 4

  await new Promise((r) => setTimeout(r, 2000));

  memoAdd(1, 2);
  console.log(count); // 5, cache expired

  memoAdd(1, 2);
  console.log(count); // 5, pulled from cache

  memoAdd(2, 4);
  console.log(count); // 6, expired value
})();

That's all folks.