Multiple .hasOwnProperty() vs Object.keys() lookups (v10)

Revision 10 of this benchmark created on


Description

All that a obj.hasOwnProperty() method can return is in Object.keys(obj). Is it faster lookup an index than inspect an object?

Setup

var obj = {
        "dateAdded": 1386900688175,
        "id": "5707",
        "index": 0,
        "parentId": "127",
        "title": "Gmail",
        "url": "http://mail.google.com/mail/u/0/#inbox"
    };
    var folder = {
        'children': [0],
        'dateAdded': 1387413393280,
        'dateGroupModified': 1387418822179,
        'id': "782",
        'index': 12,
        'parentId': "127",
        'title': "Folder"
    };
    var cachedObjKeys = Object.keys(obj),
        objKeys;

Test runner

Ready to run.

Testing in
TestOps/sec
hasOwnProperty
if (obj.hasOwnProperty('url')) {1}
if (obj.hasOwnProperty('children')) {2}
ready
Object.keys + indexOf
objKeys = Object.keys(obj);
if (~objKeys.indexOf('url')) {1}
if (~objKeys.indexOf('children')) {2}
ready
cached keys + indexOf
if(~cachedObjKeys.indexOf('url')) {1}
if(~cachedObjKeys.indexOf('children')) {2}
ready
in
if('url' in obj) {1}
if('children' in obj) {2}
ready
Key truthy if
if(obj['url']) {1}
if(obj['children']) {2}
ready
Key truthy if !==
if(obj['url'] !== undefined) {1}
if(obj['children'] !== undefined) {2}
ready
true Key if-else
if(obj['url']) {1}
else {2}
ready
true Key if-else !==
if(obj['url'] !== undefined) {1}
else {2}
ready
false key if-else
if(folder['url']) {1}
else {2}
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.