Run details:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36
Chrome 63
Windows
Other
2025 years ago
Test name Executions per second
Original defaultMemoize 51.1 Ops/sec
Optimized defaultMemoize 50.6 Ops/sec
No memoization 75.9 Ops/sec
Script Preparation code:
x
 
function defaultEqualityCheck(a, b) {
  return a === b
}
function defaultMemoize_original(func, equalityCheck = defaultEqualityCheck) {
  let lastArgs = null
  let lastResult = null
  return (...args) => {
    if (
      lastArgs === null ||
      lastArgs.length !== args.length ||
      !args.every((value, index) => equalityCheck(value, lastArgs[index]))
    ) {
      lastResult = func(...args)
    }
    lastArgs = args
    return lastResult
  }
}
function defaultMemoize_optimized(func, equalityCheck = defaultEqualityCheck) {
  const isEqual = (value, index) => equalityCheck(value, lastArgs[index])
  let lastArgs = null
  let lastResult = null
  return (...args) => {
    if (
      lastArgs === null ||
      lastArgs.length !== args.length ||
      !args.every(isEqual)
    ) {
      lastResult = func(...args)
    }
    lastArgs = args
    return lastResult
  }
}
function combine(a, b) {
  return { combined: a + b };
}
var combine_original = defaultMemoize_original(combine);
var combine_optimized = defaultMemoize_optimized(combine);
var NUM_REPEAT = 50000;
Tests:
  • Original defaultMemoize

     
    let result1 = 0;
    for (let i = 0; i < NUM_REPEAT; ++i) 
      result1 += combine_original(1, 2).combined;
  • Optimized defaultMemoize

     
    let result2 = 0;
    for (let i = 0; i < NUM_REPEAT; ++i) 
      result2 += combine_optimized(1, 2).combined;
  • No memoization

     
    let result3 = 0;
    for (let i = 0; i < NUM_REPEAT; ++i) 
      result3 += combine(1, 2).combined;