JavaScript in keyword vs property access

JavaScript performance comparison

Revision 2 of this test case created by Anonymous

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var obj = {
      hello: 4,
      wonderful: 6,
      there: 2,
      check: 13,
      library: 30,
      start: 23,
      thunder: 34,
      water: 50,
      phone: 80,
      charger: 74,
      monitor: 23,
      headphones: 35
    };
};
</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
In
var a;

if ('water' in obj) {
  a = 2 + 2;
}
pending…
property access
var a;

if (obj['water'] !== undefined) {
  a = 2 + 2;
}
pending…
brackets
var a;

if (obj.water !== undefined) {
  a = 2 + 2;
}
pending…
HasOwnProperty
var a;

if(obj.hasOwnProperty('water')) {
  a = 2 + 2;
}
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