Memoization Testing

JavaScript performance comparison

Test case created by Fred Chien(錢逢祥)

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  // Create a huge list
  const list = [];
  for (let index = 0; index < 50000; index++) {
    list.push({
  	  id: index,
  	  value: index + ' :-D'
    });
  }
  
  const tests = [];
  for (let index = 0; index < 50000; index += 5) {
    tests.push(index);
  }
  
  // Memoization
  const memoize = (fn) => {
    const cache = {};
  
    return (arg) => {
  	  if (cache[arg] === undefined) {
  		  cache[arg] = fn(arg);
  	  }
  
  	  return cache[arg];
    };
  };
  
  const getData = (list, id) => {
    return list.find((data) => {
  	  return (data.id === id);
    });
  };
  
  const normal = getData.bind(this, list);
  const cached = memoize(normal);

};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
Normal
tests.map(test => normal(test));
pending…
Memoization
tests.map(test => cached(test));
pending…
Normal (twice)
tests.map(test => normal(test));
tests.map(test => normal(test));
pending…
Memoization (twice)
tests.map(test => cached(test));
tests.map(test => cached(test));
pending…

You can edit these tests or add even more tests to this page by appending /edit to the URL.

0 Comments