最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

performance - Javascript: dictionaryobject membership check speed - Stack Overflow

programmeradmin4浏览0评论

I was curious as of what would be the fastest way to check if a JS object (used as a dictionary) has a given property.

And I was baffled by the results. See for yourself:

In Chrome, the in keyword method is 96% slower than the dot syntax. And in Firefox, it's also around 80% slower. IE shows about 50% slower

What the hell? Am I doing something wrong? I imagined the "in" keyword would be optimized, since it doesn't even need to get the value, it just returns a boolean. But apparently I was plain wrong.

I was curious as of what would be the fastest way to check if a JS object (used as a dictionary) has a given property.

And I was baffled by the results. See for yourself: http://jsperf./object-membership-check-speed/6

In Chrome, the in keyword method is 96% slower than the dot syntax. And in Firefox, it's also around 80% slower. IE shows about 50% slower

What the hell? Am I doing something wrong? I imagined the "in" keyword would be optimized, since it doesn't even need to get the value, it just returns a boolean. But apparently I was plain wrong.

Share Improve this question edited Apr 6, 2011 at 15:09 adamJLev asked Oct 27, 2010 at 16:05 adamJLevadamJLev 14k11 gold badges62 silver badges65 bronze badges 7
  • Not much difference for me in Firefox 4.0b6/Windows: array syntax is fastest, dot syntax 3% slower, 'in' keyword 10% slower. – Matthew Wilson Commented Oct 27, 2010 at 16:14
  • For a laugh, try running this in IE8. You'll get tired of closing the "This script is running slowly. Do you want to end it?" dialogs. For the record, IE8 ran ~60 times slower than Chrome. :) – Robusto Commented Oct 27, 2010 at 16:17
  • For the record, they all "get" the value, since they have to determine if such a value exists (so something has to be returned), and each way must access the specific member. – Robusto Commented Oct 27, 2010 at 16:21
  • @Robusto Yeah forget about IE.. I also wonder if the test results are flawed for IE since the timer might not stop while the dialog is open. But the in keyword is a bit different than the other methods, since it returns true even if the value of the key has been explicitly set to undefined. With the other methods I believe it's not even possible to check if something has been set explicitly or not – adamJLev Commented Oct 27, 2010 at 16:23
  • @Robusto - Regardless of whatever goes on internally, the in operator need not return the value of said property. Intuitively, in would seem the fastest as checking if a property has ever been defined would seem like a necessary first step in fetching its value, thus you'd think in would have less work to do than the alternatives. However, optimizations are rarely intuitive, and it might just be that the more mon use case is the one that is more heavily optimized. – MooGoo Commented Oct 27, 2010 at 16:29
 |  Show 2 more ments

1 Answer 1

Reset to default 7

They are not the same.

  • obj.prop will check if a property is not falsy (not null, undefined, 0, "", false).

  • prop in obj checks whether a property exists in an object (including it's prototype chain)

  • And finally you have obj.hasOwnProperty('prop') which checks if the object has prop as it's own property (can't be an inhereted one).

Example

var obj = { prop: "" };
obj.prototype = { inhereted: true };
if ( obj.prop );            // false
if ( prop in object );      // true
if ( inhereted in object ); // true
if ( obj.hasOwnProperty('prop') );      // true
if ( obj.hasOwnProperty('inhereted') ); // false

I think performance shouldn't be a problem as long as you're not doing millions of checks at a time. If you really want the fastest way though, you can use:

if ( obj.prop != null )

Which checks if the property is not null or undefined. In this form other falsy values like "" or 0 can't interfere, and you're still super performant.

发布评论

评论列表(0)

  1. 暂无评论