Complete Array Iteration (v3)

Revision 3 of this benchmark created by Daniel15 on


Preparation HTML

<script>
  Array.prototype.forEach1 = function(callback, thisArg) {
    var T = thisArg || this,
        k = 0,
        O = Object(T),
        len = O.length >>> 0;
    while (k < len) {
      var kValue;
      if (k in O) {
        kValue = O[k];
        callback.call(T, kValue, k, O);
      }
      k++;
    }
  };
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
For
var z = [],
    a = [2, 3, 4, 5, 6];
for (i = 0; i < a.length; i++) {
  if (a[i] > 3) z.push(a[i]);
}
ready
While
var z = [],
    a = [2, 3, 4, 5, 6],
    l = a.length;
while (l--) {
  if (a[l] > 3) z.push(a[l]);
}
ready
Compat forEach
var z = [];
[2, 3, 4, 5, 6].forEach1(function(e, i, a) {
  if (e > 3) z.push(e);
});
ready
Filter
var z = [];
[2, 3, 4, 5, 6].filter(function(e, i, a) {
  return e > 3;
});
ready
Map
var z = [];
[2, 3, 4, 5, 6].map(function(e, i, a) {
  return e > 3 ? e : undefined;
}).join(" ").trim().split(" ");
ready
Foreach
var z = [];
[2, 3, 4, 5, 6].forEach(function(e, i, a) {
  if (e > 3) z.push(e);
});
ready
For in
var z = [],
    a = [2, 3, 4, 5, 6];
for (i in a) {
  if (a[i] > 3) z.push(a[i]);
}
ready
For (length cached)
var z = [],
    a = [2, 3, 4, 5, 6];
for (i = 0, count = a.length; i < count; i++) {
  if (a[i] > 3) z.push(a[i]);
}
ready
For (backwards)
var z = [],
    a = [2, 3, 4, 5, 6];
for (i = a.length - 1; i >= 0; i--) {
  if (a[i] > 3) z.push(a[i]);
}
ready

Revisions

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

  • Revision 2: published by Soulcyon on
  • Revision 3: published by Daniel15 on