Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
memoizerific
Advanced tools
memoizerific is a JavaScript library that provides a simple and efficient way to memoize functions. Memoization is a technique used to speed up function execution by caching the results of expensive function calls and returning the cached result when the same inputs occur again.
Basic Memoization
This feature allows you to memoize a function with a specified cache size. In this example, the function adds two numbers and caches the result for up to 100 different input combinations.
const memoizerific = require('memoizerific');
const memoizedFunction = memoizerific(100)((a, b) => a + b);
console.log(memoizedFunction(1, 2)); // 3
console.log(memoizedFunction(1, 2)); // 3 (cached result)
Cache Size Limitation
This feature demonstrates how the cache size limitation works. When the cache exceeds the specified size, the oldest cached result is evicted.
const memoizerific = require('memoizerific');
const memoizedFunction = memoizerific(2)((a, b) => a + b);
memoizedFunction(1, 2); // Cache: {(1, 2): 3}
memoizedFunction(2, 3); // Cache: {(1, 2): 3, (2, 3): 5}
memoizedFunction(3, 4); // Cache: {(2, 3): 5, (3, 4): 7} (1, 2) is evicted
Function Argument Handling
This feature shows how memoizerific can handle functions with variable numbers of arguments. The function sums all its arguments and caches the result.
const memoizerific = require('memoizerific');
const memoizedFunction = memoizerific(100)((...args) => args.reduce((sum, val) => sum + val, 0));
console.log(memoizedFunction(1, 2, 3)); // 6
console.log(memoizedFunction(1, 2, 3)); // 6 (cached result)
lodash.memoize is a function from the Lodash library that provides similar memoization capabilities. It allows you to memoize functions and customize the cache key resolver. Compared to memoizerific, lodash.memoize is part of a larger utility library and may offer more flexibility in terms of cache key customization.
fast-memoize is a lightweight and fast memoization library. It focuses on performance and simplicity, making it a good alternative to memoizerific for scenarios where speed is critical. However, it may not offer as many features as memoizerific, such as cache size limitation.
memoizee is a comprehensive memoization library that offers a wide range of features, including cache size limitation, cache expiration, and support for various cache storage mechanisms. It is more feature-rich compared to memoizerific but may be more complex to use.
Fastest (see benchmarks), smallest (923b min/gzip), most-efficient, dependency-free, JavaScript (JS) lib to memoize functions. Fully supports multiple complex object arguments. Implements LRU (least recently used) cache to keep the most recently used results up to the provided limit. For the browser as well as node.
Memoization is the process of caching function results to be returned cheaply when the same arguments are used to call the function again.
npm install memoizerific --save
var memoizerific = require('memoizerific');
var myExpensiveFunctionMemoized = memoizerific(50)(function(arg1, arg2, arg3) {
// so many long expensive calls in here
});
myExpensiveFunction(1, 2, 3); // damn, that took looooong to process
myExpensiveFunction(1, 2, 3); // wow, that one was instant!
myExpensiveFunction(2, 3, 4); // expensive again :(
myExpensiveFunction(2, 3, 4); // woah, this one was dirt cheap, I'll take 2!
There is one option available: the max number of results to cache.
memoizerific(limit)(fn);
memoizerific(1)(function(){}); // cache 1 result
memoizerific(10000)(function(){}); // cache 10,000 results
memoizerific(0)(function(){}); // cache infinity results (not recommended)
The cache works using LRU logic (least recently used), purging the oldest results when the limit is reached.
// memoize 1 result
var myMemoized = memoizerific(1)(function(arg1, arg2, arg3, arg4) {});
myMemoized(1, 2, 3, 'a'); // function runs
myMemoized(1, 2, 3, 'a'); // cached result is returned
myMemoized(1, 2, 3, 'X'); // function runs again, new result is cached, old cached result is purged
myMemoized(1, 2, 3, 'X'); // new cached result is returned
myMemoized(1, 2, 3, 'a'); // function runs again...
This library was built for real-world use-cases, we will not be using the easy one-argument fibonacci test commonly used for memoization. Instead we will test with multiple complex arguments, that look something like this:
myMemoized(
{ a: 1, b: 2}, // complex object argument
[{ x: 'x', q: 'q', }, { b: 8, c: 9 }], // array argument
{ z: 'z' },
...
);
We generate a list of varying numbers of complex arguments (1 to 8 arguments), with varying degrees of variance, to increase and decrease cache hits and misses, then run each memoization lib through them, timing the results. For full details see the source at the memoize-js-libs-benchmarks project.
Here are results of some top projects:
// low variance, mostly cache hits:
no memoization:
memoizerific:
lodash:
erikras:
neilk:
// medium variance, lots of cache hits:
no memoization:
memoizerific:
lodash:
erikras:
neilk:
// high variance, few cache hits:
no memoization:
memoizerific:
lodash:
erikras:
neilk:
FAQs
Fast, small, efficient JavaScript memoization lib to memoize JS functions
The npm package memoizerific receives a total of 5,432,698 weekly downloads. As such, memoizerific popularity was classified as popular.
We found that memoizerific 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
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.