正在读数组的`length`财产真正的JavaScript昂贵的操作? [英] Is reading the `length` property of an array really that expensive an operation in JavaScript?

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

问题描述

我一直认为缓存在JavaScript中数组的长度是因为计算长度的价格昂贵的一个好主意(特别是在循环的条件)阵列

示例

 为(VAR I = 0; I< arr.length;我++){}// VS对于(VAR I = 0,arrLength = arr.length; I< arrLength;我++){}

不过,我想也许长度属性仅更新数组的创建和改变。因此,在阅读它不应该是相对于阅读它存储在变量(相对于在其他语言的其它方法,可能需要寻求在存储器找到的东西结束时,如 strlen的() C语言)。

我有两个问题。我也有兴趣在如何工作的,所以请不要用的 premature优化打我的棍子。

假设JavaScript引擎中的浏览器。


  1. 有什么优势缓存在JavaScript中的数组长度属性?有超过一个对象的属性读取一个局部变量更多地参与?

  2. 长度属性只是改变了对创造和移()流行()键入不返回一个新的数组否则只是存储为整数的方法?


解决方案

好吧,我会说,这是昂贵的,但后来我写了一个小测试@的 jsperf.com 并使用我吃惊的是 I< array.length 实际上是在Chrome速度更快,并在FF(4)没有关系。

我怀疑是长度存储为一个整数(UINT32)。从ECMA-规格(262 ED 5,第121页。):


  

每个数组对象都有一个
  length属性的值始终是一个非负整数小于2 32 。长度属性的值是
  数值上比的名字更大
  每个属性的名字是一个数组
  指数;每当一个数组的属性
  对象被创建或更改,其他
  特性调整为必要
  保持这种不变的。
  具体地,每当一个属性是
  加入他的名字是一个数组索引,
  length属性被改变,如果
  必要时,为比一个更多
  该数组的索引的数值;和
  每当length属性
  变了,每个属性,其名称是
  数组索引,其值不
  小于新长度是
  自动删除。这种约束
  仅适用于一个自己的属性
  数组对象是未受影响
  长度或数组索引属性
  可以由它的原型继承


唷!我不知道如果我习惯了这样的语言...

最后,我们总是有我们的浏览器背后的好老滞后。在IE(9,8,7)缓存长度为真更快。其中许多更多的理由不使用IE浏览器,我说。

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.

Example

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

// vs

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

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.

Assume the JavaScript engines in browsers.

  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?

解决方案

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.

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

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

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.

这篇关于正在读数组的`length`财产真正的JavaScript昂贵的操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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