12
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ES6での配列の連結(破壊的)

Posted at

ES6での配列の連結(破壊的)

ES6から配列が展開できるようになったため、上記記事のようにapplyを使って展開する必要がなくなりました。
下記のようにシンプルに記述できます。

実行ファイル

test_es6.js
const a = [0, 1, 2, 3], b = [4, 5, 6, 7], count = 1000000;
console.time();
for (let i = 0; i < count; i++) {
  a.push(...b);
}
console.timeEnd();
test_old.js
const a = [0, 1, 2, 3], b = [4, 5, 6, 7], count = 1000000;
console.time();
for (let i = 0; i < count; i++) {
  Array.prototype.push.apply(a, b);
}
console.timeEnd();

方法

上記のファイルをそれぞれ5回ずつ実行して計測(node v8.9.0)

結果

1回目 2回目 3回目 4回目 5回目 平均
push(...) 77.85ms 80.21ms 74.85ms 73.50ms 78.91ms 77.06ms
Array.prototype.push.apply 75.51ms 77.59ms 76.46ms 79.54ms 81.60ms 78.14ms
一応計測しましたが誤差の範囲です
12
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?