在 JavaScript 中读取数组的 `length` 属性真的那么昂贵吗? [英] Is reading the `length` property of an array really that expensive an operation in JavaScript?

查看:28
本文介绍了在 JavaScript 中读取数组的 `length` 属性真的那么昂贵吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直认为在 JavaScript 中缓存数组的长度是一个好主意(尤其是在 for 循环的条件下),因为计算数组的长度很昂贵.

I always assumed caching the length of an array in JavaScript is a good idea (especially in the condition of a for loop) because of the expensiveness of calculating the length of an array.

for (var i = 0; i < arr.length; i++) { }

// vs

for (var i = 0, arrLength = arr.length; i < arrLength; i++) { }

但是,我认为 length 属性可能仅在创建和更改数组时更新.因此,读取它不应该是一个太昂贵的操作,而不是读取它存储在变量中(与其他语言中的其他方法可能需要在内存中寻找以找到某事的结尾相反,例如 strlen() in C).

However, I thought perhaps the length property is only updated on creation and alteration of the array. Therefore, reading it shouldn't be too expensive an operation as opposed to reading it stored in a variable (as opposed to other methods in other languages that may need to seek in memory to find the end of something, e.g. strlen() in C).

我有两个问题.我也对它的工作原理很感兴趣,所以请不要用过早优化棒打击我.

I have two questions. I am also interested in how this works, so please don't hit me with the premature optimisation stick.

假设浏览器中的 JavaScript 引擎.

Assume the JavaScript engines in browsers.

  1. 在 JavaScript 中缓存数组的 length 属性有什么好处吗?在对象的属性上读取局部变量是否涉及更多内容?
  2. length 属性是否在创建时和 shift()pop() 类型方法上简单地改变了,这些方法不返回新的数组,否则简单地存储为整数?
  1. Is there any advantage to caching the length property of an array in JavaScript? Is there much more involved in reading a local variable over an object's property?
  2. Is the length property simply altered on creation and on shift() and pop() type methods that don't return a new array and otherwise simply stored as an integer?

推荐答案

好吧,我会说它很贵,但后来我写了一个小测试@jsperf.com 令我惊讶的是使用 i<array.length 在 Chrome 中实际上更快,而在 FF(4) 中它没有没关系.

Well, I would have said it was expensive, but then I wrote a little test @ jsperf.com and to my surprise using i<array.length actually was faster in Chrome, and in FF(4) it didn't matter.

我怀疑长度存储为整数(Uint32).来自 ECMA 规范(262 第 5 版,第 121 页):

My suspicion is that length is stored as an integer (Uint32). From the ECMA-specs (262 ed. 5, page 121):

每个 Array 对象都有一个length 属性,其值始终是小于 232 的非负整数.length 属性的值为数字大于名称每个名称为数组的属性指数;每当数组的属性对象被创建或更改,其他根据需要调整属性保持这个不变性.具体来说,每当一个属性添加了其名称是数组索引的,长度属性被改变,如果必要的,比该数组索引的数值;和每当长度属性是已更改,名称为的每个属性一个数组索引,其值不是小于新的长度是自动删除.这个约束仅适用于自己的属性数组对象,不受长度或数组索引属性可能继承自它的原型

Every Array object has a length property whose value is always a nonnegative integer less than 232. The value of the length property is numerically greater than the name of every property whose name is an array index; whenever a property of an Array object is created or changed, other properties are adjusted as necessary to maintain this invariant. Specifically, whenever a property is added whose name is an array index, the length property is changed, if necessary, to be one more than the numeric value of that array index; and whenever the length property is changed, every property whose name is an array index whose value is not smaller than the new length is automatically deleted. This constraint applies only to own properties of an Array object and is unaffected by length or array index properties that may be inherited from its prototypes

呸!不知道有没有习惯这样的语言...

Phew! I don't know if I ever get used to such language ...

最后,我们的浏览器总是落后于我们的老产品.在 IE (9, 8, 7) 中,缓存长度确实更快.我说,这是不使用 IE 的更多原因之一.

Finally, we always have our good old lagging behind browser. In IE (9, 8, 7) caching the length is really faster. One of many more reasons to not use IE, I say.

这篇关于在 JavaScript 中读取数组的 `length` 属性真的那么昂贵吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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