“字典模式"的优缺点 [英] Pros and Cons of "dictionary mode"

查看:280
本文介绍了“字典模式"的优缺点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,使用Javascript删除对象上的条目时,至少使用chrome时,会将对象置于字典模式"或慢速模式"

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"

示例:

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?

推荐答案

据我所知,使用Javascript删除对象上的条目时,至少使用chrome时,会将对象置于字典模式".或慢速模式"

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"

这不是JavaScript的事情;这是Chrome内V8引擎的实现特征. V8用户列表上的该主题对此进行了讨论:

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] ...使用JavaScript删除"关键字以从对象效果中删除属性(sic) v8如何优化对象? ...

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

[Sven Panne] ...删除属性会导致进入慢速模式",即使用对象属性的字典.因此,根据一般经验,使用删除"会使操作变慢...

[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...

您可以在此JSPerf中 看到这种效果.请注意,使用delete的浏览器与使用V8(Chrome,Opera 20)的浏览器相比,速度要慢. Firefox的最新SpiderMonkey两种方法都令人眼花fast乱,IE10和11受到的影响轻微. (有趣的是,Opera用于10.5至12的引擎Carakan,受delete的影响比V8还要大.)

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.)

如果您正在编写要在Web浏览器中使用的代码,则针对特定引擎进行优化往往会浪费时间,除非您在该引擎上遇到了特定的,实际的性能问题. (如果是这样,请在处理完之后,确保这些更改不会使其他引擎混乱!)

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!)

如果您是为NodeJS,SilkJS等编写的,那么针对V8进行的优化当然是可以的(尽管过早优化的常规规则仍然适用),因为这些都是V8专门构建的.

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?

不. JavaScript的语义是由规范定义的,它不规定对象的行为与规范的语义相匹配的方式.规范根本没有涉及性能. V8通过动态地生成动态类并将其编译为机器代码来实现对象,但是回退到慢"状态. (字典)模式时,从其中删除属性. (如果您 add 属性是Sven Panne在上面的引文中所说的更常见的操作,它将动态创建一个派生类,该派生类

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.

这篇关于“字典模式"的优缺点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆