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.9 to 3.0.10

137

benchmark/real-world-simulation.js

@@ -63,10 +63,29 @@

const manyArgsToString = (...args) => {
return JSON.stringify(args);
}
const sumManyArgs = (...args) => {
return args.reduce((acc, curr) => {
return acc + curr;
}, 0);
}
// Memoize each expensive function
const nanomemoizedFunction = nanomemoize(fibonacciMultipleMixed);
const fastMemoizedFunction = fastMemoize(fibonacciMultipleMixed);
const microMemoizedFunction = microMemoize(fibonacciMultipleMixed);
const moizeMemoizedFunction = microMemoize(fibonacciMultipleMixed)
const nanomemoizedFunction1 = nanomemoize(fibonacciMultipleMixed);
const fastMemoizedFunction1 = fastMemoize(fibonacciMultipleMixed);
const microMemoizedFunction1 = microMemoize(fibonacciMultipleMixed);
const moizeMemoizedFunction1 = microMemoize(fibonacciMultipleMixed);
const nanomemoizedFunction2 = nanomemoize(manyArgsToString);
const fastMemoizedFunction2 = fastMemoize(manyArgsToString);
const microMemoizedFunction2 = microMemoize(manyArgsToString);
const moizeMemoizedFunction2 = microMemoize(manyArgsToString);
const nanomemoizedFunction3 = nanomemoize(sumManyArgs);
const fastMemoizedFunction3 = fastMemoize(sumManyArgs);
const microMemoizedFunction3 = microMemoize(sumManyArgs);
const moizeMemoizedFunction3 = microMemoize(sumManyArgs);
// Set up the benchmark test

@@ -92,84 +111,70 @@ const suite = new Benchmark.Suite();

suite
.add('nanomemoizedFunction', () => {
.add('nanomemoizedFunctions', () => {
const random = Math.round(Math.random()*10);
let args;
let args1, args2
if(random<=2) {
args = [5,{}];
args1 = [5,{}];
args2 = [1,2,3,4,5,6,7,8,9,10];
} else {
args = [random,{}];
args1 = [random,{}];
args2 = [].fill(random,0,10);
}
const result = nanomemoizedFunction.apply(null,args);
const shouldBe = fibonacciMultipleMixed.apply(null,args);
if(result!==shouldBe) console.log("err nanomemoizedFunction")
assert.strictEqual(result,shouldBe);
nanomemoizedFunction1.apply(null,args1);
const result = nanomemoizedFunction2.apply(null,args2);
nanomemoizedFunction3.apply(null,args2);
const shouldBe = manyArgsToString.apply(null,args2);
if(result!==shouldBe) console.log("err nanomemoizedFunction2")
//assert.strictEqual(result,shouldBe);
})
.add('fastmoizedFunction', () => {
.add('fastMemoizedFunctions', () => {
const random = Math.round(Math.random()*10);
let args;
let args1, args2;
if(random<=2) {
args = [5,{}];
args1 = [5,{}];
args2 = [1,2,3,4,5,6,7,8,9,10];
} else {
args = [random,{}];
args1 = [random,{}];
args2 = [].fill(random,0,10);
}
const result = fastMemoizedFunction.apply(null,args);
const shouldBe = fibonacciMultipleMixed.apply(null,args);
if(result!==shouldBe) console.log("err nanomemoizedFunction")
assert.strictEqual(result,shouldBe);
const result = fastMemoizedFunction1.apply(null,args1);
fastMemoizedFunction2.apply(null,args2);
fastMemoizedFunction3.apply(null,args2);
//const shouldBe = fibonacciMultipleMixed.apply(null,args);
//if(result!==shouldBe) console.log("err nanomemoizedFunction")
//assert.strictEqual(result,shouldBe);
})
.add('microMemoizedFunction', () => {
.add('microMemoizedFunctions', () => {
const random = Math.round(Math.random()*10);
let args;
let args1, args2;
if(random<=2) {
args = [5,{}];
args1 = [5,{}];
args2 = [1,2,3,4,5,6,7,8,9,10];
} else {
args = [random,{}];
args1 = [random,{}];
args2 = [].fill(random,0,10);
}
const result = microMemoizedFunction.apply(null,args);
const shouldBe = fibonacciMultipleMixed.apply(null,args);
if(result!==shouldBe) console.log("err nanomemoizedFunction")
assert.strictEqual(result,shouldBe);
const result = microMemoizedFunction1.apply(null,args1);
microMemoizedFunction2.apply(null,args2);
microMemoizedFunction3.apply(null,args2);
//const shouldBe = fibonacciMultipleMixed.apply(null,args);
//if(result!==shouldBe) console.log("err nanomemoizedFunction")
//assert.strictEqual(result,shouldBe);
})
.add('moizeMemoizedFunction', () => {
.add('moizeMemoizedFunctions', () => {
const random = Math.round(Math.random()*10);
let args;
let args1, args2;
if(random<=2) {
args = [5,{}];
args1 = [5,{}];
args2 = [1,2,3,4,5,6,7,8,9,10];
} else {
args = [random,{}];
args1 = [random,{}];
args2 = [].fill(random,0,10);
}
const result = moizeMemoizedFunction.apply(null,args);
const shouldBe = fibonacciMultipleMixed.apply(null,args);
if(result!==shouldBe) console.log("err nanomemoizedFunction")
assert.strictEqual(result,shouldBe);
const result = moizeMemoizedFunction1.apply(null,args1);
moizeMemoizedFunction2.apply(null,args2);
moizeMemoizedFunction3.apply(null,args2);
//const shouldBe = fibonacciMultipleMixed.apply(null,args);
//if(result!==shouldBe) console.log("err nanomemoizedFunction")
//assert.strictEqual(result,shouldBe);
})
/*.add('memoizedFunction2', () => {
const arg1 = Math.random();
const arg2 = Math.random();
const result = memoizedFunction2(arg1, arg2);
assert.strictEqual(result, expensiveFunction2(arg1, arg2));
})
.add('memoizedFunction3', () => {
const arg1 = Math.random();
const arg2 = 'test';
const arg3 = { key: 'value' };
const result = memoizedFunction3(arg1, arg2, arg3);
assert.strictEqual(result, expensiveFunction3(arg1, arg2, arg3));
})
.add('memoizedFunction4', () => {
const arg1 = 'a';
const arg2 = Math.random();
const arg3 = true;
const arg4 = { key: 'value' };
const result = memoizedFunction4(arg1, arg2, arg3, arg4);
assert.deepStrictEqual(result, expensiveFunction4(arg1, arg2, arg3, arg4));
})
.add('memoizedFunction5', () => {
const arg1 = Math.random();
const arg2 = 'test';
const arg3 = { key: 'value' };
const arg4 = [1, 2, 3];
const arg5 = false;
const result = memoizedFunction5(arg1, arg2, arg3, arg4, arg5);
assert.deepStrictEqual(result, expensiveFunction5(arg1, arg2, arg3, arg4, arg5));
}) */
.on('start', () => {

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

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

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

@@ -20,9 +20,8 @@ [![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)

* For custom equality functions 'nano-memoize` and `micro-memoize vary in speed depending on the equality function used. `nano-memoize` is always first by at least 15% using `hash-it` and `micro-memoize` is always second using `hash-it` or `lodash`. See the table below.
* For custom equality functions 'nano-memoize` and `micro-memoize vary in speed depending on the equality function used. `nano-memoize` is always first by at least 15% using `hash-it` and `micro-memoize` is always second using `hash-it` or `lodash`.
* For a real world simulation where only 20% of the function calls are memoized and they take mixed argument types, `nanomemoize` remains the fastest. Although, it is within the margin of error for `fastMemoize`
* For a real world simulation where only 20% of the function calls are memoized and they take and return mixed argument types, `nanomemoize` is by far fastest. It appears this is because `nanomemoize` handles the passing of mutiple values returned as a string better than others.
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.
Starting cycles for functions with a single primitive parameter...

@@ -108,13 +107,11 @@

Starting real world simulation...
| Name | Ops / sec | Relative margin of error | Sample size |
| Name | Ops / sec | Relative margin of error | Sample size |
| ------- |------- |------------- |------- |
| nanomemoizedFunction | 3,456,663 | ± 1.10% | 92 |
| fastmoizedFunction | 3,453,148 | ± 1.69% | 92 |
| microMemoizedFunction | 1,833,970 | ± 0.80% | 95 |
| moizeMemoizedFunction | 1,810,871 | ± 0.49% | 93 |
| nanomemoizedFunctions | 3,409,618 | ± 0.99% | 94 |
| microMemoizedFunctions | 1,845,306 | ± 0.76% | 92 |
| moizeMemoizedFunctions | 1,842,258 | ± 0.82% | 90 |
| fastMemoizedFunctions | 638,857 | ± 1.30% | 94 |
If you want similar performance for intersection, union or Cartesian product also see:

@@ -134,5 +131,2 @@

The code is hand-crafted to run across all browsers all the way back to IE 11. No transpiling is necessary.
# API

@@ -180,5 +174,6 @@

# Release History (reverse chronological order)
2023-04-08 v3.0.10 Enhanced real world simulation.
2023-04-07 v3.0.9 Added real world simulation. Removed .parcel-cache from deployment.

@@ -185,0 +180,0 @@

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