Fast Memoize

Written by Ramon Gebben

fast-memoize (GitHub: caiogondim/fast-memoize.js, License: ISC, npm: fast-memoize)

First of all let me start of with informing you about why there hasn’t being a new post for several weeks now. To be completely honest with you guys, I just got very lazy. I’ve just bought my first house with the fiancée. Which of course needed to be fixed up, we needed to restore damage to the previous house and we actually needed to do the move. Besides all of that I also started at a new client which took some more time to get used to. But hey those are all excuses and you don’t really care about that. You just wanna see code. So now that I’m back and integrated into all the new environments around me I’m ready to start updating you again. So enough chit chat let’s look at fast-memoize.js.

According to Wikipedia: “In computing, memoization is an optimization technique used primarily to speed up computer programs by storing the results of expensive function calls and returning the cached result when the same inputs occur again. “.

This library is an attempt to make the fastest possible memoization library in JavaScript that supports any arguments. This is not the first attempt on doing that. There have being multiple attempts but as the developer points out those attempts are not fast enough and have limitation on number of arguments which can be passed.


The reason why Lodash comes out on top on these benchmarks is because they limit the number of arguments you can pass, thus gaining performance.

When you want to memoize a function you can do so by creating it like this:

const memoize = require('fast-memoize')
const fn = function (one, two, three) {
  // Awesome magical code in here

const memoized = memoize(fn);

memoized('foo', 3, 'bar');
// Call it again
memoized('foo', 3, 'bar'); // Cache hit

The library takes a look at the environment it in running on and selects the quickest cache to work with. If you want to implement your own cache make sure it has the following methods:

  • get
  • set
  • has
  • delete

The support is also very good for details about that you should consult the README.

If you have any interest in helping me make better, don’t hesitate to contact me with your ideas.