0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

JavaScript の配列へ empty slot 追加時の速度性能

Last updated at Posted at 2025-12-11

こんにちは。
JavaScript の配列に対し下記(empty slot 追加)を行った際に、

array.length += 1;
  • V8 利用では速度低下は見られないようですが1
  • しかし Bun (JSC) を用いてベンチマーク比較した際に顕著な速度低下が生じる場合に気がつきました(下記結果)。

一般には、実行エンジン実装の速度性能差がありますので、代わりに下記のように書く方が良さそうです。

array.push(undefined);

anti-pattern 実行時間ベンチマーク結果

下記のソースコードを実行し(n_iter = 200000)、比較すると、Bun の顕著な速度低下が見られました(なお n_iter = 100000 では見られませんでした)。

result_200000.png

source code

const n_iter = 200000;
const arr = [];
for (let i = 0; i < n_iter; i++) {
  arr.length += 1;  //  <-- low performance in the case of bun
//  arr.push(undefined);  //  <-- recommended instead of above
  arr[i] = i;
} 
  1. 極端条件では生じるだろうとは思いますが。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?