在数组的索引处分配值之前声明数组的长度是否更有效?

时间:2018-12-20 06:53:04

标签: javascript arrays

在分配值之前设置数组的长度是否有好处?

例如,

let arr = [];
arr.length = 10;
arr[0] = 'a'; // arr.length === 10
...
arr[9] = 'i'; // arr.length === 10

甚至

let arr = new Array(10);
arr[0] = 'a'; // arr.length === 10
...
arr[9] = 'i'; // arr.length === 10

let arr = [];
arr[0] = 'a'; // arr.length === 1
arr[1] = 'b'; // arr.length === 2
...
arr[9] = 'i'; // arr.length === 10

1 个答案:

答案 0 :(得分:5)

至少在V8中,看来new Array(length)比替代方法快至少一个数量级。包括push方法只是为了好玩:

(警告:运行以下代码会阻塞您的浏览器一段时间)

const t0 = performance.now();
for (let i = 0; i < 2e6; i++) {
  const arr = [];
  arr.length = 7;
  arr[0] = 'a';
  arr[1] = 'b';
  arr[2] = 'c'
  arr[3] = 'd'
  arr[4] = 'e'
  arr[5] = 'f';
  arr[6] = 'g';
}
const t1 = performance.now();
for (let i = 0; i < 2e6; i++) {
  const arr = new Array(7);
  arr[0] = 'a';
  arr[1] = 'b';
  arr[2] = 'c'
  arr[3] = 'd'
  arr[4] = 'e'
  arr[5] = 'f';
  arr[6] = 'g';
}
const t2 = performance.now();
for (let i = 0; i < 2e6; i++) {
  const arr = [];
  arr[0] = 'a';
  arr[1] = 'b';
  arr[2] = 'c'
  arr[3] = 'd'
  arr[4] = 'e'
  arr[5] = 'f';
  arr[6] = 'g';
}
const t3 = performance.now();
for (let i = 0; i < 2e6; i++) {
  const arr = [];
  arr.push('a');
  arr.push('b');
  arr.push('c');
  arr.push('d');
  arr.push('e');
  arr.push('f');
  arr.push('g');
}
const t4 = performance.now();

console.log('arr.length = length', t1 - t0);
console.log('new Array(length)', t2 - t1);
console.log('arr = [] only', t3 - t2);
console.log('push only', t4 - t3);

在Firefox上,差异并不明显,但仍然存在-new Array(length)的速度似乎是其他可能性的两倍。您可能会认为

const arr = ['a', 'b', 'c', 'd', 'e', 'f', 'g'];

甚至会更有效率,这在FF中是正确的(又高出几个数量级),但似乎与V8中的new Array(length) 大致相同

const t0 = performance.now();
for (let i = 0; i < 2e8; i++) {
  const arr = new Array(7);
  arr[0] = 'a';
  arr[1] = 'b';
  arr[2] = 'c'
  arr[3] = 'd'
  arr[4] = 'e'
  arr[5] = 'f';
  arr[6] = 'g';
}
const t1 = performance.now();
for (let i = 0; i < 2e8; i++) {
  const arr = ['a', 'b', 'c', 'd', 'e', 'f', 'g'];
}
const t2 = performance.now();

console.log('new Array(length)', t1 - t0);
console.log(`arr = ['a', 'b', ...]`, t2 - t1);