Skip to content

Instantly share code, notes, and snippets.

@Tom910
Created January 13, 2017 08:04
Show Gist options
  • Save Tom910/37d3c863103c87741d835738af03b3ba to your computer and use it in GitHub Desktop.
Save Tom910/37d3c863103c87741d835738af03b3ba to your computer and use it in GitHub Desktop.
for-cache-vs-no-cache
var Benchmark = require('benchmark');
var suite = new Benchmark.Suite;
var testArray = [];
for (var s = 0; s < 1000; s++) {
testArray.push(s * 2);
}
suite
.add('cacheTest', () => cacheTest(testArray))
.add('noCacheTest', () => noCacheTest(testArray))
.on('cycle', event => console.log(String(event.target)))
.on('complete', function() {console.log('Fastest is ' + this.filter('fastest').map('name'))})
.run({ 'async': true });
function cacheTest(array) {
var length = array.length,
result = Array(length),
i = 0;
for(; i < length; i++) {
result[i] = array[i] * 2;
}
return result;
}
function noCacheTest(array) {
var result = Array(array.length),
i = 0;
for(; i < array.length; i++) {
result[i] = array[i] * 2;
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment