Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

nano-memoize

Package Overview
Dependencies
Maintainers
1
Versions
51
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

nano-memoize - npm Package Compare versions

Comparing version 3.0.2 to 3.0.3

24

benchmark/index.js

@@ -468,2 +468,14 @@ 'use strict';

fibonacciSuite
.add('nanomemoize deep equals (lodash isEqual)', () => {
mNanoDeep(fibonacciNumber);
})
.add('nanomemoize fast equals (fast-equals deepEqual/ES6)', () => {
mNanoFastEquals(fibonacciNumber);
})
.add('nanomemoize fast deep equals (fast-deep-equal/ES6)', () => {
mNanoFastDeepEquals(fibonacciNumber);
})
.add('nanomemoize deep equals (hash-it isEqual)', () => {
mNanoHashIt(fibonacciNumber);
})
.add('micro-memoize deep equals (lodash isEqual)', () => {

@@ -478,14 +490,2 @@ mMicroMemoizeDeep(fibonacciNumber);

})
.add('nanomemoize deep equals (lodash isEqual)', () => {
mNanoDeep(fibonacciNumber);
})
.add('nanomemoize fast equals (fast-equals deepEqual)', () => {
mNanoFastEquals(fibonacciNumber);
})
.add('nanomemoize fast deep equals (fast-deep-equal/ES6)', () => {
mNanoFastDeepEquals(fibonacciNumber);
})
.add('nanomemoize deep equals (hash-it isEqual)', () => {
mNanoHashIt(fibonacciNumber);
})
.on('start', () => {

@@ -492,0 +492,0 @@ console.log(''); // eslint-disable-line no-console

@@ -80,22 +80,18 @@ function $parcel$defineInteropFlag(a) {

f = function() {
if (arguments.length || o.equals) {
var l = maxargs || arguments.length, kl = k.length, i = kl;
while(--i >= 0){
if (!maxargs && k[i].length !== l) continue; // cache miss if called with a different number of args
var j = l - 1;
while(j >= 0 && (o.equals && o.equals(k[i][j], arguments[j]) || k[i][j] === arguments[j])){
if (j === 0) return v[i];
// the args matched
j--;
}
var l = maxargs || arguments.length, kl = k.length, i = -1;
while(++i < kl){
var args = k[i];
if (maxargs != null || args.length === l) {
var j = 0;
while(j < l && eq(arguments[j], args[j]))j++;
if (j === l) return v[i];
// the args matched;
}
i = kl - (i + 1);
// set change timeout only when new value computed, hits will not push out the tte, but it is arguable they should not
return !c || c(i, v, k), v[i] = fn.apply(this, k[i] = arguments);
} // set change timeout only when new value computed, hits will not push out the tte, but it is arguable they should not
return z === undefined ? (!c || c(0, v, k), z = fn.apply(this, arguments)) : z;
}
// set change timeout only when new value computed, hits will not push out the tte, but it is arguable they should not
return !c || c(i, v, k), v[i] = fn.apply(this, k[i] = arguments);
};
// reset all the caches
f.clear = function() {
cache = new Map();
cache.clear();
s = Object.create(null);

@@ -102,0 +98,0 @@ k = [];

@@ -69,22 +69,18 @@ /*

f = function() {
if (arguments.length || o.equals) {
var l = maxargs || arguments.length, kl = k.length, i = kl;
while(--i >= 0){
if (!maxargs && k[i].length !== l) continue; // cache miss if called with a different number of args
var j = l - 1;
while(j >= 0 && (o.equals && o.equals(k[i][j], arguments[j]) || k[i][j] === arguments[j])){
if (j === 0) return v[i];
// the args matched
j--;
}
var l = maxargs || arguments.length, kl = k.length, i = -1;
while(++i < kl){
var args = k[i];
if (maxargs != null || args.length === l) {
var j = 0;
while(j < l && eq(arguments[j], args[j]))j++;
if (j === l) return v[i];
// the args matched;
}
i = kl - (i + 1);
// set change timeout only when new value computed, hits will not push out the tte, but it is arguable they should not
return !c || c(i, v, k), v[i] = fn.apply(this, k[i] = arguments);
} // set change timeout only when new value computed, hits will not push out the tte, but it is arguable they should not
return z === undefined ? (!c || c(0, v, k), z = fn.apply(this, arguments)) : z;
}
// set change timeout only when new value computed, hits will not push out the tte, but it is arguable they should not
return !c || c(i, v, k), v[i] = fn.apply(this, k[i] = arguments);
};
// reset all the caches
f.clear = function() {
cache = new Map();
cache.clear();
s = Object.create(null);

@@ -91,0 +87,0 @@ k = [];

{
"name": "nano-memoize",
"version": "v3.0.2",
"version": "v3.0.3",
"description": "Faster than fast, smaller than micro ... a nano speed and nano size memoizer.",

@@ -5,0 +5,0 @@ "type:": "module",

@@ -6,3 +6,3 @@ [![Codacy Badge](https://api.codacy.com/project/badge/Grade/30ce201484754fa5b0a6c6046abb842d)](https://www.codacy.com/app/syblackwell/nano-memoize?utm_source=github.com&amp;utm_medium=referral&amp;utm_content=anywhichway/nano-memoize&amp;utm_campaign=Badge_Grade)

Version 3.x.x of nano-memoize was modified to use newer versions of JavaScript built-in classes and take advantage of current v8 loop optimizations. As a result, the minified/brotli size at 733 bytes is slightly smaller and it is generally more than 25% faster that v2.x.x and v1.x.x.
Version 3.x.x of nano-memoize was modified to use newer versions of JavaScript built-in classes and take advantage of current v8 loop optimizations. As a result, the minified/brotli size at 728 bytes is slightly smaller and it is slightly faster that v2.x.x and v1.x.x.

@@ -13,12 +13,13 @@ Our tests show it is the nano-memoize fastest openly available JavaScript memoizer for single and multiple argument functions accepting primitives and objects. However, I have found that benchmarks can vary dramatically from O/S to O/S or Node version to Node version, so I could be wrong. These tests were run on a Windows 10 Pro 64bit 2.8ghz i7 machine with 16GB RAM and Node v18.13.0. Garbage collection was forced between each sample run to minimize its impact on results.

* For single primitive argument functions `nano-memoize` is typically 1% faster than `fast-memoize` and `micro-emoize` faster. However, all three are within the margin of error of the other.
* For single primitive argument functions `nano-memoize` is typically 2% faster than `fast-memoize` and `micro-emoize`. However, all three are within the margin of error of the other.
* For single object argument functions `nano-memoize` is typically 15% faster than its closest competitor `fast-memoize`.
* For multiple primitive argument functions `nano-memoize` is typically 5% faster than its closest competitor `moize`.
* For multiple primitive argument functions `nano-memoize`, `moize` and `micro-memoize` are always the three fastest and within each others margin of error.
* For multiple object argument functions `micro-memoize` is typically first and `nano-memoize` second. However, each are within each other's margin of error.
* For multiple object argument functions `nano-memoize`, `moize` and `micro-memoize` are always the three fastest and within each others margin of error.
* For custom equality functions 'nano-memoize` and `micro-memoize vary in speed depending on the equality function used. See the table below.
The planetheidea/moize library (which claims to be the fastest) does not include nano-memoize for comparison and the repository is not accepting comments or a pull request for some technical reason. The repository has been forked and its own benchmarking has been updated and run to confirm the results below.
The planetheidea/moize library (which claims to be the fastest on average) does not include `nano-memoize` for comparison and the repository is not accepting comments or a pull request for some technical reason. The repository has been forked and its own benchmarking has been updated and run to confirm the results below.

@@ -84,21 +85,22 @@

├───────────────┼────────────┼──────────────────────────┼─────────────┤
│ nano-memoize │ 60,955,221 │ ± 1.22% │ 88 │
│ nano-memoize │ 52,311,452 │ ± 2.17% │ 84 │
├───────────────┼────────────┼──────────────────────────┼─────────────┤
│ moize │ 53,951,415 │ ± 1.65% │ 87 │
│ moize │ 31,101,105 │ ± 19.97% │ 54 │
├───────────────┼────────────┼──────────────────────────┼─────────────┤
│ micro-memoize │ 49,832,161 │ ± 3.02% │ 83 │
│ micro-memoize │ 28,405,348 │ ± 17.84% │ 50 │
├───────────────┼────────────┼──────────────────────────┼─────────────┤
│ lru-memoize │ 31,131,459 │ ± 1.75% │ 88 │
│ lru-memoize │ 28,399,841 │ ± 2.86% │ 82 │
├───────────────┼────────────┼──────────────────────────┼─────────────┤
│ memoizee │ 18,206,413 │ ± 1.41% │ 84 │
│ memoizee │ 16,189,445 │ ± 3.63% │ 81 │
├───────────────┼────────────┼──────────────────────────┼─────────────┤
│ iMemoized │ 12,533,169 │ ± 1.34% │ 88 │
│ iMemoized │ 11,294,320 │ ± 3.18% │ 80 │
├───────────────┼────────────┼──────────────────────────┼─────────────┤
│ memoizerific │ 7,850,796 │ ± 1.36% │ 85 │
│ memoizerific │ 6,676,755 │ ± 3.79% │ 76 │
├───────────────┼────────────┼──────────────────────────┼─────────────┤
│ addy-osmani │ 4,186,545 │ ± 1.64% │ 86 │
│ addy-osmani │ 3,986,098 │ ± 3.61% │ 85 │
├───────────────┼────────────┼──────────────────────────┼─────────────┤
│ fast-memoize │ 1,066,349 │ ± 1.71% │ 88 │
│ fast-memoize │ 953,862 │ ± 4.24% │ 77 │
└───────────────┴────────────┴──────────────────────────┴─────────────┘
Starting cycles for functions with multiple parameters that contain objects...

@@ -110,2 +112,4 @@ ┌───────────────┬────────────┬──────────────────────────┬─────────────┐

├───────────────┼────────────┼──────────────────────────┼─────────────┤
│ moize │ 51,275,262 │ ± 2.11% │ 82 │
├───────────────┼────────────┼──────────────────────────┼─────────────┤
│ nano-memoize │ 49,070,846 │ ± 1.45% │ 86 │

@@ -115,4 +119,2 @@ ├───────────────┼────────────┼──────────────────────────┼─────────────┤

├───────────────┼────────────┼──────────────────────────┼─────────────┤
│ moize │ 16,383,560 │ ± 2.38% │ 67 │
├───────────────┼────────────┼──────────────────────────┼─────────────┤
│ memoizee │ 15,915,550 │ ± 4.57% │ 79 │

@@ -127,2 +129,22 @@ ├───────────────┼────────────┼──────────────────────────┼─────────────┤

Starting cycles for alternative cache types...
┌─────────────────────────────────────────────────────┬─────────────┬──────────────────────────┬─────────────┐
│ Name │ Ops / sec │ Relative margin of error │ Sample size │
├─────────────────────────────────────────────────────┼─────────────┼──────────────────────────┼─────────────┤
│ nanomemoize deep equals (hash-it isEqual) │ 112,560,448 │ ± 1.35% │ 85 │
├─────────────────────────────────────────────────────┼─────────────┼──────────────────────────┼─────────────┤
│ micro-memoize deep equals (lodash isEqual) │ 83,402,392 │ ± 2.88% │ 80 │
├─────────────────────────────────────────────────────┼─────────────┼──────────────────────────┼─────────────┤
│ micro-memoize deep equals (hash-it isEqual) │ 70,493,317 │ ± 2.46% │ 83 │
├─────────────────────────────────────────────────────┼─────────────┼──────────────────────────┼─────────────┤
│ nanomemoize deep equals (lodash isEqual) │ 63,040,211 │ ± 1.61% │ 84 │
├─────────────────────────────────────────────────────┼─────────────┼──────────────────────────┼─────────────┤
│ nanomemoize fast equals (fast-equals deepEqual/ES6) │ 61,878,364 │ ± 1.73% │ 84 │
├─────────────────────────────────────────────────────┼─────────────┼──────────────────────────┼─────────────┤
│ micro-memoize deep equals (fast-equals deepEqual) │ 56,841,180 │ ± 3.10% │ 80 │
├─────────────────────────────────────────────────────┼─────────────┼──────────────────────────┼─────────────┤
│ nanomemoize fast deep equals (fast-deep-equal/ES6) │ 41,010,681 │ ± 2.44% │ 82 │
└─────────────────────────────────────────────────────┴─────────────┴──────────────────────────┴─────────────┘
# Usage

@@ -153,5 +175,5 @@

maxArgs: number,
// go ahead and call memoized multi-args functions after a number of milliseconds via a timeout after the
// call last argument of memoized multi-args functions after a number of milliseconds via a timeout after the
// cached result has been returned, perhaps to ensure that callbacks are invoked, does not cache the timemout result
// e.g. nanomemoize(function(a,b,cb) { var result = a + b; cb(result); return result; },{maxArgs:2,callTimeout:0});
// e.g. nanomemoize(function(a,b,callback) { var result = a + b; callback(result); return result; },{maxArgs:2,callTimeout:0});
callTimeout: number,

@@ -181,4 +203,6 @@ // number of milliseconds to cache a result, set to `Infinity` or `-1` to never create timers or expire

2022-01-01 v3.0.2 Fixed https://github.com/anywhichway/nano-memoize/issues/52 with custom equals functions not consistently working. Slight performance degradation, but still generally the fastest.
2022-02-02 v3.0.3 Added unit test for `maxAge`. Adjusted varArg unit tests for more accuracy. Slight optimizations to multi argument memoized functions. Slight improvement to cache clearing that may reduce GC. Updated license file for copyright period. Updated docs on `callTimeout` for clarity.
2022-02-01 v3.0.2 Fixed https://github.com/anywhichway/nano-memoize/issues/52 with custom equals functions not consistently working. `fast-equals` or `lodash.isEqual` now work. Slight performance degradation, but still generally the fastest.
2022-01-29 v3.0.1 Fixed build issue where root index.js was not getting updated.

@@ -185,0 +209,0 @@

@@ -92,22 +92,22 @@ /*

f = function() {
if(arguments.length || o.equals) {
var l = maxargs||arguments.length, kl = k.length, i=kl;
while(--i>=0) { // an array of arrays of args, each array represents a call signature
if (!maxargs && k[i].length !== l) continue; // cache miss if called with a different number of args
var j=l-1;
while(j>=0 && ((o.equals && o.equals(k[i][j],arguments[j])) || k[i][j]===arguments[j])) { // compare each arg working back from length or args || maxargs
if(j===0) { return v[i]; } // the args matched
j--;
var l = maxargs||arguments.length, kl = k.length, i =-1;
while(++i<kl) { // k is an array of arrays of args, each array represents a call signature
var args = k[i];
if (maxargs!=null || args.length === l) {
var j = 0;
while (j < l && eq(arguments[j], args[j])) { // compare each arg
j++;
}
if (j === l) {
return v[i];
} // the args matched;
}
i = kl - (i + 1);
// set change timeout only when new value computed, hits will not push out the tte, but it is arguable they should not
return (!c||c(i,v,k)),v[i] = fn.apply(this,k[i] = arguments);
} // set change timeout only when new value computed, hits will not push out the tte, but it is arguable they should not
return z===undefined ? ((!c||c(0,v,k)),z = fn.apply(this,arguments)) : z;
};
}
// set change timeout only when new value computed, hits will not push out the tte, but it is arguable they should not
return (!c||c(i,v,k)),v[i] = fn.apply(this,k[i] = arguments);
}
}
// reset all the caches
f.clear = function() {
cache = new Map();
cache.clear();
s = Object.create(null);

@@ -114,0 +114,0 @@ k = [];

@@ -119,3 +119,4 @@ var chai,

const res2 = varArg("multi1");
expect(res1).to.not.equal(res2);
expect(res1.length).to.equal(2);
expect(res2.length).to.equal(1);
});

@@ -126,4 +127,5 @@ it("zero varg cached", function() {

const res3 = varArg();
expect(res1).to.equal(res3);
expect(res1).to.not.equal(res2);
expect(res1.length).to.equal(0);
expect(res2.length).to.equal(1);
expect(res3.length).to.equal(0);
});

@@ -140,2 +142,34 @@ it("callTimeout",function(done) {

});
it("maxAge - flush cache",function(done) {
const memoized = nanomemoize((a,b) => a + b,{maxAge: 100})
let keys = memoized.keys(),
values = memoized.values();
expect(keys.length).to.equal(0);
expect(values.length).to.equal(0);
const response = memoized(1,2);
expect(response).to.equal(3);
keys = memoized.keys();
values = memoized.values();
expect(keys.length).to.equal(1);
expect(keys[0][0]).to.equal(1);
expect(keys[0][1]).to.equal(2);
expect(values.length).to.equal(1);
expect(values[0]).to.equal(3);
setTimeout(() => {
let keys = memoized.keys(),
values = memoized.values();
expect(keys.length).to.equal(0); // cache cleared
expect(values.length).to.equal(0); // cache cleared
const response = memoized(1,3);
expect(response).to.equal(4);
keys = memoized.keys();
values = memoized.values();
expect(keys.length).to.equal(1); // new cache value
expect(keys[0][0]).to.equal(1);
expect(keys[0][1]).to.equal(3);
expect(values.length).to.equal(1);
expect(values[0]).to.equal(4);
done();
},1000)
});
it("auto-detect vArg",function() {

@@ -142,0 +176,0 @@ const arg1 = 1, arg2 = 2;

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc