function call, inner loop

JavaScript performance comparison

Test case created by Kyle Simpson

Info

Testing/profiling the code patterns from the Script Junkie article "(pre)Maturely Optimize Your JavaScript"

http://msdn.microsoft.com/en-us/scriptjunkie/gg622887.aspx

Snippet comparison #1

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
function, inner loop
function countOdds(arr) {
 var num = 0;
 for (var i = 0; i < arr.length; i++) {
  if (arr[i] % 2 == 1) num++;
 }
 return num;
}

var nums = [],
    num_odds;

// generate a list of 1000 random numbers
for (var j = 0; j < 1000; j++) {
 nums[nums.length] = Math.round(Math.random() * 10);
 num_odds = countOdds(nums);
}
pending…
no function, no inner loop
var nums = [],
    num_odds;

// generate a list of 1000 random numbers
for (var j = 0; j < 1000; j++) {
 nums[nums.length] = Math.round(Math.random() * 10);
 if (nums[nums.length - 1] % 2 == 1) num_odds++;
}
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:

1 comment

gf3 commented :

WAT?

Add a comment