javascript - 为什么用 null 初始化对象的属性会提高速度?

标签 javascript performance performance-testing

出于某种原因,似乎(至少在 Chrome 中)如果您创建一个对象并使用空值初始化每个属性,然后为每个属性分配一个值,这比最初将对象留空要快。为什么会这样(test)?

最佳答案

V8(Chrome 的 Javascript 引擎)进行了优化,可以定义隐藏类来表示 Javascript 中的特定对象。最好在创建时声明对象的属性时执行此操作。

There are two advantages to using hidden classes: property access does not require a dictionary lookup, and they enable V8 to use the classic class-based optimization, inline caching.

https://developers.google.com/v8/design#prop_access

关于javascript - 为什么用 null 初始化对象的属性会提高速度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15814923/

相关文章:

c - 使用指针 : using a global, 按值传递哪个更快?

python - global 关键字内联全局是否与 python 中的局部变量声明完全相同?

c# - 将单元测试添加到 Visual Studio 2017 负载测试

javascript - 使用jquery触发click事件动态添加href上的链接

javascript - 类型错误 : BookList is not a constructor

javascript - 与 for 循环内的 json 过滤器混淆

MySQL 性能 : Single table or multiple tables for large datasets

testing - 如何在 Jmeter 中找到准确的 "Total Testing duration time"?

mongodb - 如何使用 Scala 将 1 亿条记录加载到 MongoDB 中进行性能测试?

javascript - javascript 的 vendor 前缀