different-iteration-options

JavaScript performance comparison

Test case created by Andrei Bas

Preparation code

<script src="//cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.js"></script>

      
<script>
Benchmark.prototype.setup = function() {
  var array = [];
  array.length = 100000;
  array.fill(1);

};
</script>

Preparation code output

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
base
const newArray = [];
for (let i = 0; i < array.length; i++) {
  newArray.push(array[i] + Math.random());
}
pending…
prefetch length
const length = array.length;
const newArray = [];
for (let i = 0; i < length; i++) {
  newArray.push(array[i] + Math.random());
}
pending…
reverse
const newArray = [];
for (let i = array.length; i >= 0; i--) {
  newArray.push(array[i] + Math.random());
}
pending…
prefetched with vars
var length = array.length;
var newArray = [];
for (var i = 0; i < length; i++) {
  newArray.push(array[i] + Math.random());
}
pending…
lodash
var newArray = _.map(array, function(el) {
  return el + Math.random();
});
pending…

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

0 Comments