Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
138 views
in Technique[技术] by (71.8m points)

javascript - Pros and Cons of "dictionary mode"

It is to my knowledge that with Javascript when you delete an entry on a object, at least with chrome it puts the object into "dictionary mode" or "slow mode"

Example:

var user = { name: 'connor', sex: 'male' }; 
// user is in "fast mode"

delete user.sex;
// user is in ("slow" or "dictionary") mode 

When can this be beneficial and when can it be detrimental?

A specific case to go by would be, I have an object and when the application starts the object is empty, but as the code is run and the application memory builds up it can potentially become very large and the object will never decrease in size until the application closes by which it will not exist.

Also is there any semantics around this mode?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It is to my knowledge that with Javascript when you delete an entry on a object, at least with chrome it puts the object into "dictionary mode" or "slow mode"

That isn't a JavaScript thing; that's an implementation characteristic of the V8 engine inside Chrome. This thread on the V8 users's list discusses this:

[ZenWolf] ...Does using the JavaScript "delete" keyword to delete a property from an object effect (sic) how v8 will optimize the object? ...

[Sven Panne] ...Deleting a property results in going to "slow mode", i.e. using a dictionary for the object's properties. So as a general rule of thumb, using 'delete' makes thing slower...

You can see this effect in this JSPerf. Note how browsers that use V8 (Chrome, Opera 20) are slower with the delete than without it. Firefox's latest SpiderMonkey is blindingly fast either way, IE10 and 11 are slightly impacted. (Interestingly, the engine Opera used for 10.5 through 12 [I think it was], Carakan, was even more impacted by the delete than V8.)

If you're writing code to be used in web browsers, optimizing for a specific engine tends to be a waste of time unless you're facing a specific, real-world performance problem on that engine. (And if you are, once you've dealt with that, make sure those changes don't mess up the other engines!)

If you were writing for NodeJS, SilkJS, etc., then of course optimizing for V8 is fine (although the normal rules of premature optimization still apply), as those are built specifically with V8.

Also is there any semantics around this mode?

No. The semantics of JavaScript are defined by the specification, which doesn't dictate how objects are implemented provided their behavior matches the semantics of the spec; the spec doesn't address performance basically at all. V8 implements objects by generating dynamic classes on-the-fly and compiling them to machine code, but falling back to "slow" (dictionary) mode when you remove properties from them. (If you add properties, the much more common operation as Sven Panne said in the quote above, it dynamically creates a derived class, which doesn't slow things down.) But other engines are free to implement them as hash maps, or linked lists of properties, or anything else.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...