Memoize benchmark
// Originally from fast-memoize; updated to include `mem` and test multiple and non-primitive arguments. | |
const ora = require("ora"); | |
const Table = require("cli-table2"); | |
const debug = require("logdown")(); | |
const lruMemoize = require("lru-memoize").default; | |
const iMemoized = require("iMemoized"); | |
const Benchmark = require("benchmark"); | |
const underscore = require("underscore").memoize; | |
const lodash = require("lodash").memoize; | |
const memoizee = require("memoizee"); | |
const R = require("ramda"); | |
const nano = require("nano-memoize"); | |
const mem = require("mem"); | |
const ManyKeysMap = require("many-keys-map"); | |
const objectHash = require("object-hash"); | |
const fastMemoize = require("../src/"); | |
const results = []; | |
const spinner = ora("Running benchmark"); | |
// | |
// View | |
// | |
function showResults(benchmarkResults) { | |
const table = new Table({ | |
head: ["NAME", "OPS/SEC", "RELATIVE MARGIN OF ERROR", "SAMPLE SIZE"], | |
}); | |
benchmarkResults.forEach((result) => { | |
table.push([ | |
result.target.name, | |
result.target.hz.toLocaleString("en-US", { maximumFractionDigits: 0 }), | |
`± ${result.target.stats.rme.toFixed(2)}%`, | |
result.target.stats.sample.length, | |
]); | |
}); | |
console.log(table.toString()); | |
} | |
function sortDescResults(benchmarkResults) { | |
return benchmarkResults.sort((a, b) => (a.target.hz < b.target.hz ? 1 : -1)); | |
} | |
function onCycle(event) { | |
results.push(event); | |
ora(event.target.name).succeed(); | |
} | |
function onComplete() { | |
spinner.stop(); | |
debug.log(); | |
const orderedBenchmarkResults = sortDescResults(results); | |
showResults(orderedBenchmarkResults); | |
} | |
spinner.start(); | |
// | |
// Benchmark | |
// | |
const createTestObj = () => { | |
return { | |
id: Math.random(), | |
prop1: "value1", | |
prop2: ["value2", "value3"], | |
prop3: [{ prop4: "value4" }], | |
}; | |
}; | |
const testObj1 = createTestObj(true); | |
const testObj2 = createTestObj(true); | |
const hash = (obj1, obj2) => objectHash(obj1) + objectHash(obj2); | |
// console.log(hash(testObj1, testObj2)); | |
const memoizedUnderscore = underscore(hash); | |
const memoizedLodash = lodash(hash); | |
const memoizedMemoizee = memoizee(hash); | |
const memoizedRamda = R.memoize(hash); | |
const memoizedImemoized = iMemoized.memoize(hash); | |
const memoizedLRUSingleCache = lruMemoize(hash); | |
const memoizedLRUWithLimit = lruMemoize(9999)(hash); | |
const memoizedNano = nano(hash); | |
const memoizedMem = mem(hash); | |
const memoizedMemJSONStringify = mem(hash, { cacheKey: JSON.stringify }); | |
const memoizedMemManyKeys = mem(hash, { | |
cacheKey: (arguments_) => arguments_, | |
cache: new ManyKeysMap(), | |
}); | |
const memoizedFastMemoizeCurrentVersion = fastMemoize(hash); | |
const benchmark = new Benchmark.Suite(); | |
benchmark | |
.add("vanilla", () => { | |
hash(testObj1, testObj2); | |
}) | |
// .add("mem (incorrect)", () => { | |
// memoizedMem(testObj1, testObj2); | |
// }) | |
.add("mem (JSON.stringify)", () => { | |
memoizedMemJSONStringify(testObj1, testObj2); | |
}) | |
.add("mem (ManyKeysMap)", () => { | |
memoizedMemManyKeys(testObj1, testObj2); | |
}) | |
// .add("underscore (incorrect)", () => { | |
// memoizedUnderscore(testObj1, testObj2); | |
// }) | |
// .add("lodash (incorrect)", () => { | |
// memoizedLodash(testObj1, testObj2); | |
// }) | |
.add("memoizee", () => { | |
memoizedMemoizee(testObj1, testObj2); | |
}) | |
.add("ramda", () => { | |
memoizedRamda(testObj1, testObj2); | |
}) | |
.add("iMemoized", () => { | |
memoizedImemoized(testObj1, testObj2); | |
}) | |
// .add("lru-memoize (single cache)", () => { | |
// memoizedLRUSingleCache(testObj1, testObj2); | |
// }) | |
.add("lru-memoize (with limit)", () => { | |
memoizedLRUWithLimit(testObj1, testObj2); | |
}) | |
.add("nano-memoize", () => { | |
memoizedNano(testObj1, testObj2); | |
}) | |
.add(`fast-memoize@current`, () => { | |
memoizedFastMemoizeCurrentVersion(testObj1, testObj2); | |
}) | |
.on("cycle", onCycle) | |
.on("complete", onComplete) | |
.run({ async: true }); |
// Originally from nano-memoize; updated to include `mem` | |
"use strict"; | |
/*MIT License | |
Core benchmark code copied from micro-memoize | |
Copyright (c) 2018 Tony Quetano | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all | |
copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
SOFTWARE. | |
*/ | |
const Benchmark = require("benchmark"); | |
const Table = require("cli-table2"); | |
const ora = require("ora"); | |
const underscore = require("underscore").memoize; | |
const lodash = require("lodash").memoize; | |
const ramda = require("ramda").memoize; | |
const memoizee = require("memoizee"); | |
const fastMemoize = require("fast-memoize"); | |
const addyOsmani = require("./addy-osmani"); | |
const memoizerific = require("memoizerific"); | |
const lruMemoize = require("lru-memoize").default; | |
const moize = require("moize").default; | |
const microMemoize = require("micro-memoize"); | |
const iMemoized = require("iMemoized"); | |
const mem = require("mem"); | |
const ManyKeysMap = require("many-keys-map"); | |
const nanomemoize = require("../src/nano-memoize.js"); | |
const deepEquals = require("lodash").isEqual; | |
const fastDeepEqual = require("fast-equals").deepEqual; | |
const hashItEquals = require("hash-it").isEqual; | |
const showResults = (benchmarkResults) => { | |
const table = new Table({ | |
head: ["Name", "Ops / sec", "Relative margin of error", "Sample size"], | |
}); | |
benchmarkResults.forEach((result) => { | |
const name = result.target.name; | |
const opsPerSecond = result.target.hz.toLocaleString("en-US", { | |
maximumFractionDigits: 0, | |
}); | |
const relativeMarginOferror = `± ${result.target.stats.rme.toFixed(2)}%`; | |
const sampleSize = result.target.stats.sample.length; | |
table.push([name, opsPerSecond, relativeMarginOferror, sampleSize]); | |
}); | |
console.log(table.toString()); // eslint-disable-line no-console | |
}; | |
const sortDescResults = (benchmarkResults) => { | |
return benchmarkResults.sort((a, b) => { | |
return a.target.hz < b.target.hz ? 1 : -1; | |
}); | |
}; | |
const spinner = ora("Running benchmark"); | |
let results = []; | |
const onCycle = (event) => { | |
results.push(event); | |
ora(event.target.name).succeed(); | |
}; | |
const onComplete = () => { | |
spinner.stop(); | |
const orderedBenchmarkResults = sortDescResults(results); | |
showResults(orderedBenchmarkResults); | |
}; | |
const fibonacci = (number) => { | |
return number < 2 ? number : fibonacci(number - 1) + fibonacci(number - 2); | |
}; | |
const fibonacciObj = ({ number }) => { | |
return number < 2 | |
? number | |
: fibonacciObj({ number: number - 1 }) + | |
fibonacciObj({ number: number - 2 }); | |
}; | |
const fibonacciMultiplePrimitive = (number, isComplete) => { | |
if (isComplete) { | |
return number; | |
} | |
const firstValue = number - 1; | |
const secondValue = number - 2; | |
return ( | |
fibonacciMultiplePrimitive(firstValue, firstValue < 2) + | |
fibonacciMultiplePrimitive(secondValue, secondValue < 2) | |
); | |
}; | |
const fibonacciMultipleObject = (number, check) => { | |
if (check.isComplete) { | |
return number; | |
} | |
const firstValue = number - 1; | |
const secondValue = number - 2; | |
return ( | |
fibonacciMultipleObject(firstValue, { | |
...check, | |
isComplete: firstValue < 2, | |
}) + | |
fibonacciMultipleObject(secondValue, { | |
...check, | |
isComplete: secondValue < 2, | |
}) | |
); | |
}; | |
const fibonacciMultipleDeepEqual = ({ number }) => { | |
return number < 2 | |
? number | |
: fibonacciMultipleDeepEqual({ number: number - 1 }) + | |
fibonacciMultipleDeepEqual({ number: number - 2 }); | |
}; | |
const runSingleParameterSuite = () => { | |
const fibonacciSuite = new Benchmark.Suite("Single parameter"); | |
const fibonacciNumber = 35; | |
const mUnderscore = underscore(fibonacci); | |
const mLodash = lodash(fibonacci); | |
// const mRamda = ramda(fibonacci); | |
const mMemoizee = memoizee(fibonacci); | |
const mFastMemoize = fastMemoize(fibonacci); | |
const mAddyOsmani = addyOsmani(fibonacci); | |
const mMemoizerific = memoizerific(Infinity)(fibonacci); | |
const mLruMemoize = lruMemoize(Infinity)(fibonacci); | |
const mMoize = moize(fibonacci); | |
const mMicroMemoize = microMemoize(fibonacci); | |
const mIMemoized = iMemoized.memoize(fibonacci); | |
const mNano = nanomemoize(fibonacci); | |
const mMem = mem(fibonacci); | |
const mMemJSONStringify = mem(fibonacci, { cacheKey: JSON.stringify }); | |
const mMemManyKeysMap = mem(fibonacci, { | |
cacheKey: (args) => args, | |
cache: new ManyKeysMap(), | |
}); | |
return new Promise((resolve) => { | |
fibonacciSuite | |
.add("nano-memoize", () => { | |
mNano(fibonacciNumber); | |
}) | |
.add("mem", () => { | |
mMem(fibonacciNumber); | |
}) | |
.add("mem (JSON.stringify)", () => { | |
mMemJSONStringify(fibonacciNumber); | |
}) | |
.add("mem (ManyKeysMap)", () => { | |
mMemManyKeysMap(fibonacciNumber); | |
}) | |
.add("addy-osmani", () => { | |
mAddyOsmani(fibonacciNumber); | |
}) | |
.add("lodash", () => { | |
mLodash(fibonacciNumber); | |
}) | |
.add("lru-memoize", () => { | |
mLruMemoize(fibonacciNumber); | |
}) | |
.add("memoizee", () => { | |
mMemoizee(fibonacciNumber); | |
}) | |
.add("memoizerific", () => { | |
mMemoizerific(fibonacciNumber); | |
}) | |
/*.add('ramda', () => { | |
mRamda(fibonacciNumber); | |
})*/ | |
.add("underscore", () => { | |
mUnderscore(fibonacciNumber); | |
}) | |
.add("iMemoized", () => { | |
mIMemoized(fibonacciNumber); | |
}) | |
.add("micro-memoize", () => { | |
mMicroMemoize(fibonacciNumber); | |
}) | |
.add("moize", () => { | |
mMoize(fibonacciNumber); | |
}) | |
.add("fast-memoize", () => { | |
mFastMemoize(fibonacciNumber); | |
}) | |
.on("start", () => { | |
console.log(""); // eslint-disable-line no-console | |
console.log( | |
"Starting cycles for functions with a single primitive parameter..." | |
); // eslint-disable-line no-console | |
results = []; | |
spinner.start(); | |
}) | |
.on("cycle", onCycle) | |
.on("complete", () => { | |
onComplete(); | |
resolve(); | |
}) | |
.run({ | |
async: true, | |
}); | |
}); | |
}; | |
const runSingleParameterObjectSuite = () => { | |
const fibonacciSuite = new Benchmark.Suite("Single parameter"); | |
const fibonacciNumber = { number: 35 }; | |
const mUnderscore = underscore(fibonacciObj); | |
const mLodash = lodash(fibonacciObj); | |
// const mRamda = ramda(fibonacciObj); | |
const mMemoizee = memoizee(fibonacciObj); | |
const mFastMemoize = fastMemoize(fibonacciObj); | |
const mAddyOsmani = addyOsmani(fibonacciObj); | |
const mMemoizerific = memoizerific(Infinity)(fibonacciObj); | |
const mLruMemoize = lruMemoize(Infinity)(fibonacciObj); | |
const mMoize = moize(fibonacciObj); | |
const mMicroMemoize = microMemoize(fibonacciObj); | |
const mIMemoized = iMemoized.memoize(fibonacciObj); | |
const mNano = nanomemoize(fibonacciObj, { relaxed: true }); | |
const mMem = mem(fibonacciObj); | |
const mMemJSONStringify = mem(fibonacciObj, { cacheKey: JSON.stringify }); | |
const mMemManyKeysMap = mem(fibonacciObj, { | |
cacheKey: (args) => args, | |
cache: new ManyKeysMap(), | |
}); | |
return new Promise((resolve) => { | |
fibonacciSuite | |
.add("nano-memoize", () => { | |
mNano(fibonacciNumber); | |
}) | |
.add("mem", () => { | |
mMem(fibonacciNumber); | |
}) | |
.add("mem (JSON.stringify)", () => { | |
mMemJSONStringify(fibonacciNumber); | |
}) | |
.add("mem (ManyKeysMap)", () => { | |
mMemManyKeysMap(fibonacciNumber); | |
}) | |
.add("addy-osmani", () => { | |
mAddyOsmani(fibonacciNumber); | |
}) | |
.add("lodash", () => { | |
mLodash(fibonacciNumber); | |
}) | |
.add("lru-memoize", () => { | |
mLruMemoize(fibonacciNumber); | |
}) | |
.add("memoizee", () => { | |
mMemoizee(fibonacciNumber); | |
}) | |
.add("memoizerific", () => { | |
mMemoizerific(fibonacciNumber); | |
}) | |
/*.add('ramda', () => { | |
mRamda(fibonacciNumber); | |
})*/ | |
.add("underscore", () => { | |
mUnderscore(fibonacciNumber); | |
}) | |
.add("iMemoized", () => { | |
mIMemoized(fibonacciNumber); | |
}) | |
.add("micro-memoize", () => { | |
mMicroMemoize(fibonacciNumber); | |
}) | |
.add("moize", () => { | |
mMoize(fibonacciNumber); | |
}) | |
.add("fast-memoize", () => { | |
mFastMemoize(fibonacciNumber); | |
}) | |
.on("start", () => { | |
console.log(""); // eslint-disable-line no-console | |
console.log( | |
"Starting cycles for functions with a single object parameter..." | |
); // eslint-disable-line no-console | |
results = []; | |
spinner.start(); | |
}) | |
.on("cycle", onCycle) | |
.on("complete", () => { | |
onComplete(); | |
resolve(); | |
}) | |
.run({ | |
async: true, | |
}); | |
}); | |
}; | |
const runMultiplePrimitiveSuite = () => { | |
const fibonacciSuite = new Benchmark.Suite("Multiple parameters (Primitive)"); | |
const fibonacciNumber = 35; | |
const isComplete = false; | |
const mMemoizee = memoizee(fibonacciMultiplePrimitive); | |
const mFastMemoize = fastMemoize(fibonacciMultiplePrimitive); | |
const mAddyOsmani = addyOsmani(fibonacciMultiplePrimitive); | |
const mMemoizerific = memoizerific(Infinity)(fibonacciMultiplePrimitive); | |
const mLruMemoize = lruMemoize(Infinity)(fibonacciMultiplePrimitive); | |
const mMoize = moize(fibonacciMultiplePrimitive); | |
const mMicroMemoize = microMemoize(fibonacciMultiplePrimitive); | |
const mIMemoized = iMemoized.memoize(fibonacciMultiplePrimitive); | |
const mNano = nanomemoize(fibonacciMultiplePrimitive); | |
const mMem = mem(fibonacciMultiplePrimitive, { | |
cacheKey: (args) => args.join(","), | |
}); | |
const mMemJSONStringify = mem(fibonacciMultiplePrimitive, { | |
cacheKey: JSON.stringify, | |
}); | |
const mMemManyKeysMap = mem(fibonacciMultiplePrimitive, { | |
cacheKey: (args) => args, | |
cache: new ManyKeysMap(), | |
}); | |
return new Promise((resolve) => { | |
fibonacciSuite | |
.add("nano-memoize", () => { | |
mNano(fibonacciNumber, isComplete); | |
}) | |
.add("mem", () => { | |
mMem(fibonacciNumber, isComplete); | |
}) | |
.add("mem (JSON.stringify)", () => { | |
mMemJSONStringify(fibonacciNumber, isComplete); | |
}) | |
.add("mem (ManyKeysMap)", () => { | |
mMemManyKeysMap(fibonacciNumber, isComplete); | |
}) | |
.add("addy-osmani", () => { | |
mAddyOsmani(fibonacciNumber, isComplete); | |
}) | |
.add("lru-memoize", () => { | |
mLruMemoize(fibonacciNumber, isComplete); | |
}) | |
.add("memoizee", () => { | |
mMemoizee(fibonacciNumber, isComplete); | |
}) | |
.add("iMemoized", () => { | |
mIMemoized(fibonacciNumber, isComplete); | |
}) | |
.add("memoizerific", () => { | |
mMemoizerific(fibonacciNumber, isComplete); | |
}) | |
.add("fast-memoize", () => { | |
mFastMemoize(fibonacciNumber, isComplete); | |
}) | |
.add("micro-memoize", () => { | |
mMicroMemoize(fibonacciNumber, isComplete); | |
}) | |
.add("moize", () => { | |
mMoize(fibonacciNumber, isComplete); | |
}) | |
.on("start", () => { | |
console.log(""); // eslint-disable-line no-console | |
console.log( | |
"Starting cycles for functions with multiple parameters that contain only primitives..." | |
); // eslint-disable-line no-console | |
results = []; | |
spinner.start(); | |
}) | |
.on("cycle", onCycle) | |
.on("complete", () => { | |
onComplete(); | |
resolve(); | |
}) | |
.run({ | |
async: true, | |
}); | |
}); | |
}; | |
const runMultipleObjectSuite = () => { | |
const fibonacciSuite = new Benchmark.Suite("Multiple parameters (Object)"); | |
const fibonacciNumber = 35; | |
const isComplete = { | |
isComplete: false, | |
}; | |
const mMemoizee = memoizee(fibonacciMultipleObject); | |
const mFastMemoize = fastMemoize(fibonacciMultipleObject); | |
const mAddyOsmani = addyOsmani(fibonacciMultipleObject); | |
const mMemoizerific = memoizerific(Infinity)(fibonacciMultipleObject); | |
const mLruMemoize = lruMemoize(Infinity)(fibonacciMultipleObject); | |
const mMoize = moize(fibonacciMultipleObject); | |
const mMicroMemoize = microMemoize(fibonacciMultipleObject); | |
const mNano = nanomemoize(fibonacciMultipleObject); | |
const mMemJSONStringify = mem(fibonacciMultipleObject, { | |
cacheKey: JSON.stringify, | |
}); | |
const mMemManyKeysMap = mem(fibonacciMultipleObject, { | |
cacheKey: (args) => args, | |
cache: new ManyKeysMap(), | |
}); | |
return new Promise((resolve) => { | |
fibonacciSuite | |
.add("nano-memoize", () => { | |
mNano(fibonacciNumber, isComplete); | |
}) | |
.add("mem (JSON.stringify)", () => { | |
mMemJSONStringify(fibonacciNumber, isComplete); | |
}) | |
.add("mem (ManyKeysMap)", () => { | |
mMemManyKeysMap(fibonacciNumber, isComplete); | |
}) | |
.add("addy-osmani", () => { | |
mAddyOsmani(fibonacciNumber, isComplete); | |
}) | |
.add("lru-memoize", () => { | |
mLruMemoize(fibonacciNumber, isComplete); | |
}) | |
.add("memoizee", () => { | |
mMemoizee(fibonacciNumber, isComplete); | |
}) | |
.add("memoizerific", () => { | |
mMemoizerific(fibonacciNumber, isComplete); | |
}) | |
.add("fast-memoize", () => { | |
mFastMemoize(fibonacciNumber, isComplete); | |
}) | |
.add("micro-memoize", () => { | |
mMicroMemoize(fibonacciNumber, isComplete); | |
}) | |
.add("moize", () => { | |
mMoize(fibonacciNumber, isComplete); | |
}) | |
.on("start", () => { | |
console.log(""); // eslint-disable-line no-console | |
console.log( | |
"Starting cycles for functions with multiple parameters that contain objects..." | |
); // eslint-disable-line no-console | |
results = []; | |
spinner.start(); | |
}) | |
.on("cycle", onCycle) | |
.on("complete", () => { | |
onComplete(); | |
resolve(); | |
}) | |
.run({ | |
async: true, | |
}); | |
}); | |
}; | |
const runMultipleLargeObjectSuite = () => { | |
const fibonacciSuite = new Benchmark.Suite( | |
"Multiple parameters (Large Object)" | |
); | |
const fibonacciNumber = 35; | |
const isComplete = { | |
isComplete: false, | |
prop0: "value0", | |
prop1: ["value1", "value2"], | |
prop2: ["value3", { prop3: "value4" }], | |
prop4: "value5", | |
prop5: undefined, | |
}; | |
const mMemoizee = memoizee(fibonacciMultipleObject); | |
const mFastMemoize = fastMemoize(fibonacciMultipleObject); | |
const mAddyOsmani = addyOsmani(fibonacciMultipleObject); | |
const mMemoizerific = memoizerific(Infinity)(fibonacciMultipleObject); | |
const mLruMemoize = lruMemoize(Infinity)(fibonacciMultipleObject); | |
const mMoize = moize(fibonacciMultipleObject); | |
const mMicroMemoize = microMemoize(fibonacciMultipleObject); | |
const mNano = nanomemoize(fibonacciMultipleObject); | |
const mMemJSONStringify = mem(fibonacciMultipleObject, { | |
cacheKey: JSON.stringify, | |
}); | |
const mMemManyKeysMap = mem(fibonacciMultipleObject, { | |
cacheKey: (args) => args, | |
cache: new ManyKeysMap(), | |
}); | |
return new Promise((resolve) => { | |
fibonacciSuite | |
.add("nano-memoize", () => { | |
mNano(fibonacciNumber, isComplete); | |
}) | |
.add("mem (JSON.stringify)", () => { | |
mMemJSONStringify(fibonacciNumber, isComplete); | |
}) | |
.add("mem (ManyKeysMap)", () => { | |
mMemManyKeysMap(fibonacciNumber, isComplete); | |
}) | |
.add("addy-osmani", () => { | |
mAddyOsmani(fibonacciNumber, isComplete); | |
}) | |
.add("lru-memoize", () => { | |
mLruMemoize(fibonacciNumber, isComplete); | |
}) | |
.add("memoizee", () => { | |
mMemoizee(fibonacciNumber, isComplete); | |
}) | |
.add("memoizerific", () => { | |
mMemoizerific(fibonacciNumber, isComplete); | |
}) | |
.add("fast-memoize", () => { | |
mFastMemoize(fibonacciNumber, isComplete); | |
}) | |
.add("micro-memoize", () => { | |
mMicroMemoize(fibonacciNumber, isComplete); | |
}) | |
.add("moize", () => { | |
mMoize(fibonacciNumber, isComplete); | |
}) | |
.on("start", () => { | |
console.log(""); // eslint-disable-line no-console | |
console.log( | |
"Starting cycles for functions with multiple parameters that contain *large* objects..." | |
); // eslint-disable-line no-console | |
results = []; | |
spinner.start(); | |
}) | |
.on("cycle", onCycle) | |
.on("complete", () => { | |
onComplete(); | |
resolve(); | |
}) | |
.run({ | |
async: true, | |
}); | |
}); | |
}; | |
const runMultipleVeryLargeObjectSuite = () => { | |
const fibonacciSuite = new Benchmark.Suite( | |
"Multiple parameters (Very Large Object)" | |
); | |
const fibonacciNumber = 35; | |
const isComplete = { | |
isComplete: false, | |
prop0: "value0", | |
prop1: ["value1", "value2"], | |
prop2: ["value3", { prop3: "value4" }], | |
prop4: "value5", | |
prop5: undefined, | |
nested: { | |
prop0: "value0", | |
prop1: ["value1", "value2"], | |
prop2: ["value3", { prop3: "value4" }], | |
prop4: "value5", | |
prop5: undefined, | |
nested: { | |
prop0: "value0", | |
prop1: ["value1", "value2"], | |
prop2: ["value3", { prop3: "value4" }], | |
prop4: "value5", | |
prop5: undefined, | |
nested: { | |
prop0: "value0", | |
prop1: ["value1", "value2"], | |
prop2: ["value3", { prop3: "value4" }], | |
prop4: "value5", | |
prop5: undefined, | |
nested: { | |
prop0: "value0", | |
prop1: ["value1", "value2"], | |
prop2: ["value3", { prop3: "value4" }], | |
prop4: "value5", | |
prop5: undefined, | |
nested: { | |
prop0: "value0", | |
prop1: ["value1", "value2"], | |
prop2: ["value3", { prop3: "value4" }], | |
prop4: "value5", | |
prop5: undefined, | |
}, | |
}, | |
}, | |
}, | |
}, | |
}; | |
const mMemoizee = memoizee(fibonacciMultipleObject); | |
const mFastMemoize = fastMemoize(fibonacciMultipleObject); | |
const mAddyOsmani = addyOsmani(fibonacciMultipleObject); | |
const mMemoizerific = memoizerific(Infinity)(fibonacciMultipleObject); | |
const mLruMemoize = lruMemoize(Infinity)(fibonacciMultipleObject); | |
const mMoize = moize(fibonacciMultipleObject); | |
const mMicroMemoize = microMemoize(fibonacciMultipleObject); | |
const mNano = nanomemoize(fibonacciMultipleObject); | |
const mMemJSONStringify = mem(fibonacciMultipleObject, { | |
cacheKey: JSON.stringify, | |
}); | |
const mMemManyKeysMap = mem(fibonacciMultipleObject, { | |
cacheKey: (args) => args, | |
cache: new ManyKeysMap(), | |
}); | |
return new Promise((resolve) => { | |
fibonacciSuite | |
.add("nano-memoize", () => { | |
mNano(fibonacciNumber, isComplete); | |
}) | |
.add("mem (JSON.stringify)", () => { | |
mMemJSONStringify(fibonacciNumber, isComplete); | |
}) | |
.add("mem (ManyKeysMap)", () => { | |
mMemManyKeysMap(fibonacciNumber, isComplete); | |
}) | |
.add("addy-osmani", () => { | |
mAddyOsmani(fibonacciNumber, isComplete); | |
}) | |
.add("lru-memoize", () => { | |
mLruMemoize(fibonacciNumber, isComplete); | |
}) | |
.add("memoizee", () => { | |
mMemoizee(fibonacciNumber, isComplete); | |
}) | |
.add("memoizerific", () => { | |
mMemoizerific(fibonacciNumber, isComplete); | |
}) | |
.add("fast-memoize", () => { | |
mFastMemoize(fibonacciNumber, isComplete); | |
}) | |
.add("micro-memoize", () => { | |
mMicroMemoize(fibonacciNumber, isComplete); | |
}) | |
.add("moize", () => { | |
mMoize(fibonacciNumber, isComplete); | |
}) | |
.on("start", () => { | |
console.log(""); // eslint-disable-line no-console | |
console.log( | |
"Starting cycles for functions with multiple parameters that contain *very large* objects..." | |
); // eslint-disable-line no-console | |
results = []; | |
spinner.start(); | |
}) | |
.on("cycle", onCycle) | |
.on("complete", () => { | |
onComplete(); | |
resolve(); | |
}) | |
.run({ | |
async: true, | |
}); | |
}); | |
}; | |
// const runAlternativeOptionsSuite = () => { | |
// const fibonacciSuite = new Benchmark.Suite("Multiple parameters (Object)"); | |
// const fibonacciNumber = { | |
// number: 35, | |
// }; | |
// const mMicroMemoizeDeep = microMemoize(fibonacciMultipleDeepEqual, { | |
// isEqual: deepEquals, | |
// }); | |
// const mMicroMemoizeFastDeep = microMemoize(fibonacciMultipleDeepEqual, { | |
// isEqual: fastDeepEqual, | |
// }); | |
// const mMicroMemoizeHashIt = microMemoize(fibonacciMultipleDeepEqual, { | |
// isEqual: hashItEquals, | |
// }); | |
// const mNanoDeep = nanomemoize(fibonacciMultipleDeepEqual, { | |
// equals: deepEquals, | |
// }); | |
// const mNanoFastDeep = nanomemoize(fibonacciMultipleDeepEqual, { | |
// equals: fastDeepEqual, | |
// }); | |
// const mNanoHashIt = nanomemoize(fibonacciMultipleDeepEqual, { | |
// equals: hashItEquals, | |
// }); | |
// return new Promise((resolve) => { | |
// fibonacciSuite | |
// .add("nanomemoize deep equals (lodash isEqual)", () => { | |
// mNanoDeep(fibonacciNumber); | |
// }) | |
// .add("nanomemoize deep equals (fast-equals deepEqual)", () => { | |
// mNanoFastDeep(fibonacciNumber); | |
// }) | |
// .add("nanomemoize deep equals (hash-it isEqual)", () => { | |
// mNanoHashIt(fibonacciNumber); | |
// }) | |
// .add("micro-memoize deep equals (lodash isEqual)", () => { | |
// mMicroMemoizeDeep(fibonacciNumber); | |
// }) | |
// .add("micro-memoize deep equals (fast-equals deepEqual)", () => { | |
// mMicroMemoizeFastDeep(fibonacciNumber); | |
// }) | |
// .add("micro-memoize deep equals (hash-it isEqual)", () => { | |
// mMicroMemoizeHashIt(fibonacciNumber); | |
// }) | |
// .on("start", () => { | |
// console.log(""); // eslint-disable-line no-console | |
// console.log("Starting cycles for alternative cache types..."); // eslint-disable-line no-console | |
// results = []; | |
// spinner.start(); | |
// }) | |
// .on("cycle", onCycle) | |
// .on("complete", () => { | |
// onComplete(); | |
// resolve(); | |
// }) | |
// .run({ | |
// async: true, | |
// }); | |
// }); | |
// }; | |
// runAlternativeOptionsSuite(); | |
//runMultiplePrimitiveSuite(); | |
runSingleParameterSuite() | |
.then(runSingleParameterObjectSuite) | |
.then(runMultiplePrimitiveSuite) | |
.then(runMultipleObjectSuite) | |
.then(runMultipleLargeObjectSuite) | |
.then(runMultipleVeryLargeObjectSuite); | |
// .then(runAlternativeOptionsSuite); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment