Accessing Object properties

JavaScript performance comparison

Revision 2 of this test case created

Info

What is the fastest way to access an object property?

obj.property or obj['property'] ?

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var obj = {
        property: 'value',
        test: 12345,
        'anothertest': [1, 2, 3, 4],
        hohoho: {
            kkk: 'hehehe',
            yeye: 'glu glug'
        },
        end: 'final',
    };
    var obj2 = { end: 'aaa','foo-bar': 'known'};
};
</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
Direct access
obj.end1 = 1
pending…
Direct string access
obj['end2'] = 1
pending…
nonstandard name
obj['foo-bar'] = 1
pending…
clojure keyword
obj['\uFDD0:foo'] = 1
pending…
known
obj.end = 1
pending…
known 2
obj['end'] = 1
pending…
keywordosymbol
obj['\uFDD0\uFDD1foo'] = 1
pending…
known nonstandard
obj2['foo-bar'] = 1
pending…
known fast?
obj2['end'] = 1
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