Y.Array.each vs. for vs. foreach

JavaScript performance comparison

Test case created by Nazmul Hassan

Info

Do a comparison of - YUI's Y.Array.each - native for loop - native foreach loop

Preparation code

<script type="text/javascript" src="//yui.yahooapis.com/combo?3.3.0/build/yui/yui-min.js"></script>
<script>
  var iterations = 30;
  var arr = [];
  var sum = 0;
  var i = 0;
 
  while (i < iterations) {
   arr.push(i++);
  }
 
  Y = YUI().use('lang');
</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
Y.Array.each(arr, function(v) {
 sum = sum + v;
});
pending…
Native for loop
for (var i = 0; i<arr.length; i++) {
  sum = sum + arr[i];
}
pending…
Native foreach loop
arr.forEach(function() {
  sum = sum + arr[i];
});
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