Compiler Optimization

JavaScript performance comparison

Revision 2 of this test case created by niftymonkey

Info

This test is meant to illustrate that there is a lot of work the compiler does to optimize the data structures when it sees that your function is consistently being called with the same types of values.

As you can see, the code in each test is exactly the same with one exception. In the first, we call the function with just numbers. In the second test, we call the function with just strings. In the last, we call the function with numbers AND strings.

Moral of the story is, don't mix your types in your function usage if you want the compiler to optimize the structures.

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
Just Numbers
function add(a, b) {
  return a + b;
}

for(var i = 0; i<100000; i++) {
  add(1, 2);
  add(3, 4);
  add(5, 6);
}
pending…
Just Strings
function add(a, b) {
  return a + b;
}

for(var i = 0; i<100000; i++) {
  add("a", "b");
  add("c", "d");
  add("e", "f");
}
pending…
Numbers AND Strings
function add(a, b) {
  return a + b;
}

for(var i = 0; i<100000; i++) {
  add(1, 2);
  add(3, 4);
  add("A", "B");
}
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