V8如何优化超大型阵列的创建? [英] How does V8 optimise the creation of very large arrays?

查看:64
本文介绍了V8如何优化超大型阵列的创建?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,我不得不优化一项涉及创建非常大的数组(〜10个元素)的任务.

Recently, I had to work on optimising a task that involved the creation of really large arrays (~ 10⁸ elements).

我测试了几种不同的方法,据jsperf所述,以下选项似乎最快.

I tested a few different methods, and, according to jsperf, the following option seemed to be the fastest.

var max = 10000000;
var arr = new Array(max);
for (let i = 0; i < max; i++) {
  arr[i] = true;
}

比那快〜85%

var max = 10000000;
var arr = [];
for (let i = 0; i < max; i++) {
  arr.push(true);
}

实际上,在我的实际应用中,第一个代码片段也要快得多.

And indeed, the first snippet was much faster in my actual app as well.

但是,我的理解是 V8引擎能够执行优化的操作在具有 PACKED_SMI_ELEMENTS 元素类型的数组上,而不是 HOLEY_ELEMENTS 的数组.

However, my understanding was that the V8 engine was able to perform optimised operations on array with PACKED_SMI_ELEMENTS elements kind, as opposed to arrays of HOLEY_ELEMENTS.

所以我的问题如下:

  • 如果确实是 new Array(n)创建了一个内部标记有 HOLEY_ELEMENTS (我相信是真的)和
  • 的数组
  • 如果确实是 [] 创建一个内部标记有 PACKED_SMI_ELEMENTS 的数组(我不太确定是真的)
  • if it's true that new Array(n) creates an array that's internally marked with HOLEY_ELEMENTS, (which I believe is true) and
  • if it's true that [] creates an array that's internally marked with PACKED_SMI_ELEMENTS (which I'm not too sure is true)

为什么第一个代码片段比第二个要快?

我曾经经历过的相关问题:

Related questions I've been through:

创建广告素材的最有效方法零填充JavaScript数组?

推荐答案

V8开发人员在此处.第一个代码段速度更快,因为 new Array(max)通知V8您想要数组的大小,因此它可以立即分配大小合适的数组;而在第二个包含 [] / .push()的代码段中,数组从零容量开始,并且必须增长多次,这包括将其现有元素复制到新的后备商店.

V8 developer here. The first snippet is faster because new Array(max) informs V8 how big you want the array to be, so it can allocate an array of the right size immediately; whereas in the second snippet with []/.push(), the array starts at zero capacity and has to be grown several times, which includes copying its existing elements to a new backing store.

https://www.youtube.com/watch?v=m9cTaYI95Zc 是一个好的演示文稿,但可能应该更清楚地说明填充元素和多孔元素之间的性能差异有多小,您应该为此担心的程度很少.

https://www.youtube.com/watch?v=m9cTaYI95Zc is a good presentation but probably should have made it clearer how small the performance difference between packed and holey elements is, and how little you should worry about it.

简而言之:只要您知道需要多大的数组,就可以使用 new Array(n)将其预分配给该大小.当您不知道最终要有多大时,然后从一个空数组开始(使用 [] new Array() new Array(0),无关紧要)并根据需要进行扩展(使用 a.push(...) a[a.length] = ... ,没关系).

In short: whenever you know how big you need an array to be, it makes sense to use new Array(n) to preallocate it to that size. When you don't know in advance how large it's going to be in the end, then start with an empty array (using [] or new Array() or new Array(0), doesn't matter) and grow it as needed (using a.push(...) or a[a.length] = ..., doesn't matter).

旁注:使用new Array()和push进行for循环"基准测试创建的数组要大两倍.

Side note: your "for loop with new Array() and push" benchmark creates an array that's twice as big as you want.

这篇关于V8如何优化超大型阵列的创建?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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