🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more

memoizee

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

memoizee

Memoize/cache function results

0.4.17
latest
Version published
Weekly downloads
4.2M
-7.19%
Maintainers
1
Weekly downloads
 
Created

What is memoizee?

The memoizee npm package is a complete memoization library for JavaScript. It allows for caching the results of function calls based on the arguments provided. This can significantly improve performance for expensive function calls that are invoked repeatedly with the same parameters.

What are memoizee's main functionalities?

Basic memoization

This feature allows you to memoize any function, caching its results so that subsequent calls with the same arguments return immediately without recomputing.

const memoize = require('memoizee');
const expensiveFunction = (arg) => { /* complex computation */ };
const memoized = memoize(expensiveFunction);

Memoization with options

This feature allows you to customize the memoization behavior with options such as maxAge for cache expiration, preFetch for renewing cache just before it expires, and max for limiting the number of cache entries.

const memoize = require('memoizee');
const memoized = memoize(expensiveFunction, { maxAge: 1000, preFetch: 0.5, max: 10 });

Primitive mode

Primitive mode ensures that the cache keys are based on the value of the arguments rather than their reference, which is useful for arguments that are primitive values.

const memoize = require('memoizee');
const memoized = memoize(expensiveFunction, { primitive: true });

Asynchronous function memoization

This feature allows memoization of asynchronous functions, caching the resolved value of the promise.

const memoize = require('memoizee');
const expensiveAsyncFunction = async (arg) => { /* complex async computation */ };
const memoized = memoize(expensiveAsyncFunction, { promise: true });

WeakMap-based memoization

This feature uses WeakMap to store cache entries, which allows for garbage collection of non-referenced keys, useful for memoizing functions that accept objects as arguments.

const memoize = require('memoizee/weak');
const memoized = memoize(expensiveFunction);

Other packages similar to memoizee

FAQs

Package last updated on 24 May 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts