Created
January 13, 2017 08:04
-
-
Save Tom910/37d3c863103c87741d835738af03b3ba to your computer and use it in GitHub Desktop.
for-cache-vs-no-cache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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