Skip to content

Instantly share code, notes, and snippets.

@jackmott
Last active March 28, 2017 15:08
Show Gist options
  • Select an option

  • Save jackmott/295c7c883aba2e4b3ad2586ffa7320aa to your computer and use it in GitHub Desktop.

Select an option

Save jackmott/295c7c883aba2e4b3ad2586ffa7320aa to your computer and use it in GitHub Desktop.
javascript map vs imperative testing
<script>
//All tests run repeatedly until numbers stabilize (jit warmup)
//Node 7.7.4 NODE_ENV = production - 42ms
//Firefox 52.0.1 32bit 250 to 280ms
//Firefox 52.0.1 64bit 210 to 250 ms
//Chrome 57.0.2987.110 (64-bit) 45ms
function testImperative(values)
{
var result = new Array(values.length);
for (var i = 0; i < values.length;i++){
var x = values[i];
result[i] = x*x;
}
return result
}
//Node 7.7.4 NODE_ENV = production - 986msms after JIT WARMUP
// Firefox 52.0.1 32bit 250 to 280 ms
// Firefox 52.0.1 64bit 210 to 250 ms
//Chrome 57.0.2987.110 (64-bit) 2000ms
function testMap(values) {
return values.map(x => x*x);
}
//If you drop this down to 1 million Firefox
//performs comparably to node/chrome
//and map and imperative still tie
var testArray = new Array(10000000);
for (var i = 0; i < testArray.length;i++)
{
testArray[i] = i;
}
for (var i = 0; i < 10; i++)
{
var startTime = new Date().getTime();
var x = testImperative(testArray);
var endTime = new Date().getTime();
console.log(endTime-startTime);
console.log('map');
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment