JavaScript 对象中键查找的性能

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7700987/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 03:14:31  来源:igfitidea点击:

Performance of key lookup in JavaScript object

javascriptdictionaryhash

提问by Saher Ahwal

I just read this question: are there dictionaries in javascript like python?

我刚刚读到这个问题:javascript 中是否有像 python 一样的字典?

One of the answers said that you can use JavaScript objects like Python dictionaries. Is that true? What is the performance of a key lookup in an object? Is it O(1)? Is adding a key to the object also constant time (hashing)?

其中一个答案说你可以使用像 Python 字典这样的 JavaScript 对象。真的吗?在对象中进行键查找的性能如何?是 O(1) 吗?向对象添加键是否也是恒定时间(散列)?

回答by Domenic

The V8 design docsimply lookups will be at least this fast, if not faster:

V8设计文档暗示查询将至少这快,如果不是更快:

Most JavaScript engines use a dictionary-like data structure as storage for object properties- each property access requires a dynamic lookup to resolve the property's location in memory. This approach makes accessing properties in JavaScript typically much slower than accessing instance variables in programming languages like Java and Smalltalk. In these languages, instance variables are located at fixed offsets determined by the compiler due to the fixed object layout defined by the object's class. Access is simply a matter of a memory load or store, often requiring only a single instruction.

To reduce the time required to access JavaScript properties, V8 does not use dynamic lookup to access properties. Instead, V8 dynamically creates hidden classes behind the scenes. [...] In V8, an object changes its hidden class when a new property is added.

大多数 JavaScript 引擎使用类似字典的数据结构作为对象属性的存储- 每个属性访问都需要动态查找来解析属性在内存中的位置。这种方法使得在 JavaScript 中访问属性通常比在 Java 和 Smalltalk 等编程语言中访问实例变量慢得多。在这些语言中,由于对象类定义的固定对象布局,实例变量位于由编译器确定的固定偏移量处。访问只是内存加载或存储的问题,通常只需要一条指令。

为了减少访问 JavaScript 属性所需的时间,V8 不使用动态查找来访问属性。相反,V8 在幕后动态创建隐藏类。[...]在 V8 中,当添加新属性时,对象会更改其隐藏类。

It sounds like adding a new key might be slightly slower, though, due to the hidden class creation.

不过,由于隐藏类的创建,听起来添加新密钥可能会稍微慢一些。

回答by Peter

Yes, you can assume that adding a key, and later using it for access are effectivelyconstant time operations.

是的,您可以假设添加密钥,然后将其用于访问是有效的恒定时间操作。

Under the hood the JS engine may apply some techniques to optimize subsequent lookups, but for the purposes of any algorithm, you can assume O(1).

在幕后,JS 引擎可能会应用一些技术来优化后续查找,但对于任何算法,您都可以假设 O(1)。

回答by Valeriy Katkov

Take a look at a similar question How does JavaScript VM implements Object property access?and my answer. Here I described the optimization technique used by JS engines and how it affects the key lookup performance. I hope that knowing of these details let you write more efficient JS code.

看一个类似的问题JavaScript VM 如何实现对象属性访问?和我的回答。在这里我描述了 JS 引擎使用的优化技术以及它如何影响关键查找性能。希望了解了这些细节,能让你写出更高效的JS代码。