Loop increment vs decrement (v4)

Revision 4 of this benchmark created by Erwan Legrand on


Description

Test the performance of copying an array via an incrementing loop vs. a decrementing loop.

Preparation HTML

<script>
  var source, dest;
  source = [];
  for (var index = 0; index < 1000; ++index) {
   source[index] = index;
  }
</script>

Setup

dest = null;

Test runner

Ready to run.

Testing in
TestOps/sec
Increment
dest = [];
for (var index = 0; index < source.length; ++index) {
 dest[index] = source[index];
}
ready
Cached length increment
dest = [];
for (var index = 0, length = source.length; index < length; ++index) {
 dest[index] = source[index];
}
ready
Decrement
dest = [];
for (var index = source.length - 1; index; --index) {
 dest[index] = source[index];
}
ready
forEach
dest = [];
source.forEach((function (d){return function (e, i, a) {
        var x = d;
        x[i] = e;
};})(dest));
ready
While decrement
dest = [];
var index = source.length;
while (index--) {
 dest[index] = source[index];
}
ready
.slice(0)
dest = source.slice(0);
ready

Revisions

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

  • Revision 4: published by Erwan Legrand on