util.memoize

A utility wrapper for memoizing expensive functions

Usage no npm install needed!

<script type="module">
  import utilMemoize from 'https://cdn.skypack.dev/util.memoize';
</script>

README

util.memoize

A utility wrapper for memoizing expensive functions

build analysis code style: prettier testing NPM

Installation

This module uses yarn to manage dependencies and run scripts for development.

To install as an application dependency:

$ yarn add util.memoize

To build the app and run all tests:

$ yarn run all

Overview

Memoization is used to cache the results of an expensive function call, where the same parameters to the function will always give the same return (idempotent).

import memoize from "util.memoize";
const ret = memoize(3, 3, (x: number, y: number) => {return x + y});

e.g. the memoize function can be called over and over again, but the given function is only executed the first time it is called. All subsequent calls use a cached return value of that first call.

The last argument to the memoize() should be the function call whose return value will be cached. The same function above can be rewritten without using an anonymous function to avoid the performance hit of regenerating the function over and over with:

import memoize from "util.memoize";

function adder(x: number, y: number): number {
    return x + y;
}

const ret = memoize(3, 3, adder);

The library also has a function named memoizeFn that returns a function reference that can be saved and used repeatedly. This is a more traditional syntax to memoize a function (like memoize-one.

import {memoizeFn} from "util.memoize";

function adder(x: number, y: number): number {
    return x + y;
}

const memoizedAdder = memoizeFn(adder);

for (let i = 0; i < 5; i++) {
    const ret = memoizedAdder(3, 3);  // 6
}

This works like the previous example in that after the first call all subsequent calls are a cached return. The memoizeFn() is just a convenience wrapper for the memoize().

Note that this library should only be used to cache functions that are expensive to call. The memoize() contains overhead within to hash the parameters and function implementation to serve as a unique identifier for the cache. If the hash is more expensive to compute than the function call, then don't use memoization.

API