什么是JavaScript对象/阵列的性能? (专门为谷歌V8) [英] What is the performance of Objects/Arrays in JavaScript? (specifically for Google V8)

查看:165
本文介绍了什么是JavaScript对象/阵列的性能? (专门为谷歌V8)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在JavaScript中(尤其是谷歌V8)数组和对象相关联的表现会是非常有趣的记录。我觉得关于这个主题没有COM prehensive文章在互联网上的任何地方。

Performance associated with Arrays and Objects in JavaScript (especially Google V8) would be very interesting to document. I find no comprehensive article on this topic anywhere on the Internet.

据我所知,有些物体使用类作为其底层的数据结构。如果有很多的属性,它有时哈希表处理?

I understand that some Objects use classes as their underlying data structure. If there are a lot of properties, it is sometimes treated as a hash table?

我也明白,数组有时像C ++阵列(即快速随机索引,慢删除和调整)处理。而且,其他时候,他们的待遇更像对象(快速索引,快速插入/拔出,更多的内存)。而且,也许有时它们被存储链表(即慢随机索引,快速移除/插入开头/结束)

I also understand that Arrays are sometimes treated like C++ Arrays (i.e. fast random indexing, slow deletion and resizing). And, other times, they are treated more like Objects (fast indexing, fast insertion/removal, more memory). And, maybe sometimes they are stored as linked lists (i.e. slow random indexing, fast removal/insertion at the beginning/end)

什么是JavaScript的数组/对象检索和操作的precise表现?(专门为谷歌V8)

更具体地,它的性能的影响:

More specifically, what it the performance impact of:


  • 添加属性的对象

  • 从删除对象中的属性

  • 在对象索引属性

  • 添加项的Array

  • 从数组中的项目

  • 在一个数组索引项

  • 调用Array.pop()

  • 调用的Array.push()

  • 调用Array.shift()

  • 调用Array.unshift()

  • 调用Array.slice()

有关详细信息的任何文章或链接将AP preciated,以及。 :)

Any articles or links for more details would be appreciated, as well. :)

编辑:我真的想知道的JavaScript数组和对象如何引擎盖下工作。此外,在什么样的背景的确实的V8引擎知道到切换到另一个数据结构?

I am really wondering how JavaScript arrays and objects work under the hood. Also, in what context does the V8 engine "know" to "switch-over" to another data structure?

例如,假设我创建...一个数组

For example, suppose I create an array with...

var arr = [];
arr[10000000] = 20;
arr.push(21);

什么是真正回事?

What's really going on here?

或...这个...什么???

Or... what about this...???

var arr = [];
//Add lots of items
for(var i = 0; i < 1000000; i++)
    arr[i] = Math.random();
//Now I use it like a queue...
for(var i = 0; i < arr.length; i++)
{
    var item = arr[i].shift();
    //Do something with item...
}

对于传统阵列,性能将是可怕的;然而,如果使用一个链表......没有那么糟糕。

For conventional arrays, the performance would be terrible; whereas, if a LinkedList was used... not so bad.

推荐答案

更新:的请注意,JS preF是目前下跌

UPDATE: Note that JSPref is currently down

(我已经保存了测试用例的副本,而一旦JS preF是固定的将更新接听/继任者被发现)

嗯......也许是因为答案是矫枉过正...但我创建的测试套件,precisely探讨这些问题(及以上)(<一个href=\"http://web.archive.org/web/20150907055636/http://jsperf.com/object-vs-array-vs-native-linked-list\"相对=nofollow>存档的副本)。

Hmm... maybe an overkill for the answer... but I created a test suite, precisely to explore these issues (and more) (archived copy).

在这个意义上,你可以看到在这50多个测试用例测试仪的性能问题(这将需要很长的时间)。

And in that sense, you can see the performance issues in this 50+ test case tester (it will take a long time).

另外,作为它的名字暗示,它探讨使用DOM结构的本机链表性质的使用。

Also as its name suggest, it explores the usage of using the native linked list nature of the DOM structure.

(目前下跌,重建过程中)我的博客上关于此的更多细节

作为跟随摘要


  • V8数组是快,速度非常快

  • 阵列推/流行/移约〜+ 20倍比任何对象等同更快。

  • 令人惊讶的 Array.shift()是快〜6倍左右不是一个数组的流行慢,但大约〜100倍不是一个对象的属性更快删除。

  • 有趣的是,的Array.push(数据); 为近20比数组[nextIndex] =数据快倍。

  • Array.unshift(数据)是预期的慢,并且是〜5倍左右比一个新的属性添加慢。

  • 归零值数组[索引] = NULL 比删除更快删除阵列[指数] (未定义)在约〜4倍++更快的数组。

  • 在物体令人惊讶归零的值 OBJ [ATTR] = NULL 〜约2倍慢于只删除属性删除OBJ [ATTR]

  • 不出所料,中数组方法Array.splice(指数为0,数据)很慢,很慢。

  • 出人意料的是,方法Array.splice(指数,1,数据)进行了优化(无长度变化),是快100倍不仅仅是拼接阵列.splice(指数为0,数据)

  • 勿庸置疑,在divLinkedList不如对所有部门的数组,除了 dll.splice(指数,1)删除(如果它打破了测试系统)。

  • 最大的惊喜这一切[作为jjrv指出],V8阵列写操作略快于V8读取= O

  • V8 Array is Fast, VERY FAST
  • Array push / pop / shift is ~approx 20x+ faster than any object equivalent.
  • Surprisingly Array.shift() is fast ~approx 6x slower than an array pop, but is ~approx 100x faster than an object attribute deletion.
  • Amusingly, Array.push( data ); is faster than Array[nextIndex] = data by almost 20 times over.
  • Array.unshift(data) is slower as expected, and is ~approx 5x slower than a new property adding.
  • Nulling the value array[index] = null is faster than deleting it delete array[index] (undefined) in an array by ~approx 4x++ faster.
  • Surprisingly Nulling a value in an object is obj[attr] = null ~approx 2x slower than just deleting the attribute delete obj[attr]
  • Unsurprisingly, mid array Array.splice(index,0,data) is slow, very slow.
  • Surprisingly, Array.splice(index,1,data) has been optimized (no length change) and is 100x faster than just splice Array.splice(index,0,data)
  • unsurprisingly, the divLinkedList is inferior to an array on all sectors, except dll.splice(index,1) removal (Where it broke the test system).
  • BIGGEST SURPRISE of it all [as jjrv pointed out], V8 array writes are slightly faster than V8 reads =O

注意:这些指标只适用于大阵/对象而V8不完全优化了。这里可以非常孤立优化的性能的情况下对数组/对象的大小小于任意大小(24?)。更多的细节可以广泛地跨多个谷歌IO视频中可以看出。

Note: These metrics applies only to large array/objects which v8 does not "entirely optimise out". There can be very isolated optimised performance cases for array/object size less then an arbitrary size (24?). More details can be seen extensively across several google IO videos.

注意2:这些精彩的表现结果不跨浏览器共享,特别是
*咳嗽* IE浏览器。另外,测试是巨大的,因此,我还没有全面分析和评估结果:请=编辑)

Note 2: These wonderful performance results are not shared across browsers, especially *cough* IE. Also the test is huge, hence i yet to fully analyze and evaluate the results : please edit it in =)

更新注(2012年12月):谷歌重新presentatives有描述(当它从一个链表数组转换成固定数组,等等等等)铬本身的内部运作youtubes视频,以及如何对其进行优化。请参见 GDC 2012:从控制台到Chrome 了解。

Updated Note (dec 2012): Google representatives have videos on youtubes describing the inner workings of chrome itself (like when it switches from a linkedlist array to a fixed array, etc), and how to optimize them. See GDC 2012: From Console to Chrome for more.

更新注意事项(2月2013年): THX @badunk,在确切点提供视频链接

Updated Note (feb 2013): Thx @badunk, for providing the video link at the exact point

这篇关于什么是JavaScript对象/阵列的性能? (专门为谷歌V8)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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