Compiler Type Optimization

JavaScript performance comparison

Test case created by niftymonkey


This test is meant to test the type optimization that the compiler can do when it sees that your function is consistently being called with the same types of values.

As you can see, this is using the same simple "add" function (which is defined in the setup). The difference is as follows:

In Chrome, it does seem to get MUCH slower when you call it with both types ... and that the optimization is evident. However, the results in Firefox are completely different as it seems Firefox optimizes even that. Even though Safari is significantly slower, it seems to optimize all of these options equivalently (much like FF does). And in IE10, the performance it too terrible to attempt to draw a conclusion.

Preparation code

Benchmark.prototype.setup = function() {
    // number of times to run each test
    var i, iterations = 100000;
    // simple add functions that add the first argument to the second
    function addT1(a, b) {
      return a + b;
    function addT2(a, b) {
      return a + b;
    function addT3(a, b) {
      return a + b;

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
for(i = 0; i < iterations; i++) {
  addT1(1, 2);
  addT1(3, 4);
  addT1(5, 6);
Just Strings
for(i = 0; i < iterations; i++) {
  addT2("a", "b");
  addT2("c", "d");
  addT2("e", "f");
Numbers AND Strings
for(i = 0; i < iterations; i++) {
  addT3(1, 2);
  addT3(3, 4);
  addT3("A", "B");

Compare results of other browsers


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:


Comment form temporarily disabled.

Add a comment