Research
Security News
Threat Actor Exposes Playbook for Exploiting npm to Build Blockchain-Powered Botnets
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.
fast-memoize
Advanced tools
The fast-memoize npm package is a high-performance memoization library that caches the results of function calls, so that the result can be quickly retrieved when the function is called again with the same arguments. This can significantly improve performance for functions with expensive computations or I/O operations that are called repeatedly with the same inputs.
Simple memoization
This feature allows you to memoize a simple function like the Fibonacci sequence. The memoized version of the function will remember the results of previous calls and return the cached result when the same input occurs again.
const memoize = require('fast-memoize');
const fibonacci = n => (n < 2 ? n : fibonacci(n - 1) + fibonacci(n - 2));
const memoizedFibonacci = memoize(fibonacci);
console.log(memoizedFibonacci(10));
Custom cache
This feature allows you to provide a custom cache implementation. By default, fast-memoize uses a plain JavaScript object as a cache, but you can replace it with any other object that follows the Map interface, such as a Map instance.
const memoize = require('fast-memoize');
const myCache = new Map();
const memoizedFn = memoize(fn, { cache: { create: () => myCache } });
Custom serializer
This feature allows you to provide a custom serializer for the arguments of the memoized function. By default, fast-memoize uses a serializer that can handle primitives and object references, but with a custom serializer, you can extend this to handle more complex serialization scenarios.
const memoize = require('fast-memoize');
const jsonSerializer = { serialize: JSON.stringify };
const memoizedFn = memoize(fn, { serializer: jsonSerializer });
Custom strategy
This feature allows you to provide a custom memoization strategy. fast-memoize comes with a default strategy that works well for most cases, but you can implement your own strategy for more control over how memoization is applied.
const memoize = require('fast-memoize');
const customStrategy = require('my-custom-strategy');
const memoizedFn = memoize(fn, { strategy: customStrategy });
lodash.memoize is a memoization function from the popular Lodash utility library. It is similar to fast-memoize but may not be as performance-focused. Lodash allows for custom cache implementations but is generally considered to be larger and more feature-rich than fast-memoize.
memoizee is another npm package that offers memoization. It provides a rich set of features, including the ability to limit cache size, expire cached items, and primitive mode for arguments handling. It is more configurable than fast-memoize but might be slower due to its additional features.
mem is a minimalist memoization library that can cache the results of functions with any type of arguments, not just primitives. It offers TTL (time-to-live) for cache entries and is designed to be simple and efficient, though it may not be as fast as fast-memoize for certain use cases.
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. — Wikipedia
This library is an attempt to make the fastest possible memoization library in JavaScript that supports N arguments.
There are already very popular solutions for this problem, but they are not very fast enough or accept only one argument.
To use the library, install it through npm
npm install fast-memoize
To port it to Browser or any other (non CJS) environment, use your favorite CJS bundler. No favorite yet? Try: Browserify, Webmake or Webpack
const memoize = require('fast-memoize')
const fn = function (one, two, three) { /* ... */ }
const memoized = memoize(fn)
memoized('foo', 3, 'bar')
memoized('foo', 3, 'bar') // Cache hit
There is already plenty of libraries that does memoization on JS world. underscore and lodash provides it, but they don't accept more than one argument. memoizee is a very well written library that supports N arguments, but is not even close on performance to lodash.
Below you can see a performance benchmark between some of the most popular libraries for memoization.
fast-memoize is faster than any other library but lodash. The reason why is that lodash does not support N arguments and is very optimized to that unique use case. But even though, fast-memoize is the library that supports N that comes closer to it.
To run the benchmark, clone the repo, install the dependencies and run npm run benchmark
.
git clone git@github.com:caiogondim/fast-memoize.git
cd fast-memoize
npm install
npm run benchmark
FAQs
Fastest memoization lib that supports N arguments
The npm package fast-memoize receives a total of 842,526 weekly downloads. As such, fast-memoize popularity was classified as popular.
We found that fast-memoize demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Research
Security News
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.
Security News
NVD’s backlog surpasses 20,000 CVEs as analysis slows and NIST announces new system updates to address ongoing delays.
Security News
Research
A malicious npm package disguised as a WhatsApp client is exploiting authentication flows with a remote kill switch to exfiltrate data and destroy files.