Y.Array.each vs. forEach vs. for vs. for-in

JavaScript performance comparison

Revision 10 of this test case created by vincent piel

Info

Do a comparison of - YUI's Y.Array.each - native forEach loop - native forEach loop cached - native for loop - native for loop w/ length variable - native for-in loop - native while loop - reverse while loop with index

NOTE: The original "native while loop" test was flawed in that it modified the array so that subsequent runs worked off an empty array. The fix would be to clone the array before each test, preferably outside the timed invocation. Unfortunately, it doesn't appear jsperf runs the test setup before each and every invocation.. and for good reason (see https://github.com/mathiasbynens/jsperf.com/issues/90). In order to prevent the array clone from unfairly throwing off the two tests that modify the array, I've gone ahead and put the array clone code in each and every test so that they are all offset by the same amount.

Preparation code

<script type="text/javascript" src="//yui.yahooapis.com/combo?3.7.3/build/yui/yui-min.js"></script>
<script>
  var iterations;
  var arr;
  var originalArr;
  var sum;
  var i;
  var total;
 
  iterations = 1000;
  arr = [];
  sum = 0;
  i = 0;
  total = 0;
 
  while (i < iterations) {
   i++;
   arr.push(i);
   total += i;
  }
  originalArr = arr.slice(0);

  Y = YUI().use('lang');

sum = 0;

var addSum = function(v) {
  sum += v;
};

</script>

Preparation code output

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
Y.Array.each
sum = 0;
Y.Array.each(originalArr , function(v) {
 sum += v;
});
if (sum !== total) {
  throw new Error("total != ");
}
pending…
Native forEach loop
sum = 0;
originalArr.forEach(function(v) {
  sum += v;
});
if (sum !== total) {
  throw new Error("total != ");
}
pending…
Native for loop
arr = originalArr.slice(0);
sum = 0;

for (var i = 0; i< arr.length; i++) {
  sum += arr[i];
}
if (sum !== total) {
  throw new Error("total != ");
}
pending…
Native for loop w/ length variable
sum = 0;
for (var i = 0, len = originalArr.length; i < len; i++) {
 sum += originalArr[i];
}
if (sum !== total) {
  throw new Error("total != ");
}
pending…
Native for-in loop
sum = 0;
for (var i in arr) {
  if (originalArr.hasOwnProperty(i)) {
    sum += originalArr[i];
  }
}
if (sum !== total) {
  throw new Error("total != ");
}
pending…
Native while loop
arr = originalArr.slice(0);
sum = 0;
var item;
while (item = arr.shift()){
  sum += item;
}
if (sum !== total) {
  throw new Error("total != ");
}
pending…
arr = originalArr.slice(0);
sum = 0;
var item;
while(item = Array.prototype.shift.call(arr)){
   sum += item;
}
if (sum !== total) {
  throw new Error("total != ");
}
pending…
Reverse while
var idx = originalArr.length;
sum = 0;
while(idx--) {
  sum += originalArr[idx];
}
if (sum !== total) {
  throw new Error("total != ");
}
pending…
Native for loop with prefix operator
sum = 0;
for (var i = 0; i<originalArr.length; ++i) {
  sum += originalArr[i];
}
if (sum !== total) {
  throw new Error("total != ");
}
pending…
Native for loop with cached length and prefix operator
sum = 0;
for (var i = 0, len = originalArr.length; i < len; ++i) {
 sum += originalArr[i];
}
if (sum !== total) {
  throw new Error("total != ");
}
pending…
Reverse native for loop with cached length and prefix operator
sum = 0;
for (var i = originalArr.length - 1; i >= 0; --i) {
 sum += originalArr[i];
}
if (sum !== total) {
  throw new Error("total != ");
}
pending…
Native pop loop
sum = 0;
arr = originalArr.slice(0);
var item;
while (item = arr.pop()){
  sum += item;
}
if (sum !== total) {
  throw new Error("total != ");
}
pending…
- native forEach loop cached
sum = 0;
originalArr.forEach(addSum);
if (sum !== total) {
  throw new Error("total != ");
}
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:

0 comments

Add a comment