function call, inner loop

JavaScript performance comparison

Revision 3 of this 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

Preparation code

<script>
  function isOdd(num) {
   return (num % 2 == 1);
  }
 
  var num_odds, nums, i;
</script>

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
nums = [];
num_odds = 0;

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

// generate a list of 1000 random numbers
for (i = 0; i < 1000; i++) {
 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:

0 comments

Add a comment