lookup

JavaScript performance comparison

Revision 4 of this test case created by WebReflection

Preparation code

<script>
  var random = Math.random;
  var t1 = function() {
   return random();
  };
  var t2 = (function() {
   return function() {
    return random();
   };
  }());
  var t5 = (function() {
   return (function() {
    return (function() {
     return (function() {
      return function() {
       return random();
      };
     }());
    }());
   }());
  }());
  
  var t10 = (function() {
   return (function() {
    return (function() {
     return (function() {
      return (function() {
       return (function() {
        return (function() {
         return (function() {
          return (function() {
           return function() {
            return random();
           };
          }());
         }());
        }());
       }());
      }());
     }());
    }());
   }());
  }());
</script>
    

Test runner

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

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
1 lookup
var f = t1();
pending…
2 lookup
var f = t2();
pending…
5 lookups
var f = t5();
pending…
10 lookups
var f = t10();
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.

1 Comment

Mariusz Nowak commented :

So in Revision 1, real cost was extra function calls and not scope lookup. Scope lookup cost is hardly noticeable in this test, and won't affect performance in real world app.