5
2

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: 配列複製の速度比較

Last updated at Posted at 2024-09-07

spread構文とArray系関数の複製速度の比較をしてみます。

benchmark 1

配列1匹分
for(let loop=1<<20,size=2,n=10;n--;loop/=2,size*=2){
	let A=Array(size).fill(1),t=new Date;
	for(let a=loop,b;a--;)b=[...A];
	t=new Date-t;
	let u=new Date;
	for(let a=loop,b;a--;)b=A.concat();
	let v=new Date;
	for(let a=loop,b;a--;)b=A.slice();
	let w=new Date;
	for(let a=loop,b;a--;)b=Array.from(A);
	console.log("loop:",loop,"size:",size,"time:",t,new Date-u,new Date-v,new Date-w)
}

Chrome(128.0.6613.113)

loop size spread concat slice from
1048576 2 392 659 616 585
524288 4 276 380 359 345
262144 8 217 259 249 241
131072 16 186 188 182 176
65536 32 177 159 155 152
32768 64 171 145 142 140
16384 128 166 134 132 130
8192 256 168 130 128 126
4096 512 163 129 127 125
2048 1024 165 132 130 128

loop数固定、配列長巨大化

loop size spread concat slice from
2048 2 0 3 2 2
2048 6 1 4 3 3
2048 18 4 5 4 4
2048 54 10 9 8 8
2048 162 27 28 28 27
2048 486 79 63 62 61
2048 1458 254 189 187 184
2048 4374 687 532 524 516
2048 13122 1971 1574 1553 1533
2048 39366 6540 5998 5634 5269

Firefox(97.0.1)

loop size spread concat slice from
1048576 2 116 595 422 338
524288 4 100 342 240 197
262144 8 97 292 187 150
131072 16 111 197 124 107
65536 32 72 161 97 89
32768 64 73 128 80 75
16384 128 105 237 154 114
8192 256 151 371 246 158
4096 512 133 298 196 134
2048 1024 117 239 148 109

loop数固定、配列長巨大化

loop size spread concat slice from
2048 2 3 2 1 1
2048 6 4 1 2 2
2048 18 2 4 2 2
2048 54 4 7 5 4
2048 162 13 26 18 15
2048 486 51 102 73 59
2048 1458 188 292 219 153
2048 4374 410 869 550 411
2048 13122 1258 2572 1577 1278
2048 39366 4169 8879 5419 4264

benchmark 2

配列2匹分
for(let loop=1<<20,size=2,n=10;n--;loop/=2,size*=2){
	let A=Array(size).fill(1),t=new Date;
	for(let a=loop,b;a--;)b=[...A,...A];
	t=new Date-t;
	let u=new Date;
	for(let a=loop,b;a--;)b=A.concat(A.concat());
	console.log("loop:",loop,"size:",size,"time:",t,new Date-u)
}

Chrome

loop size spread concat
1048576 2 487 378
524288 4 320 188
262144 8 245 97
131072 16 206 54
65536 32 198 29
32768 64 202 16
16384 128 197 12
8192 256 194 8
4096 512 191 8
2048 1024 189 7

loop数固定、配列長巨大化

loop size spread concat
2048 2 3 1
2048 6 4 1
2048 18 10 1
2048 54 17 1
2048 162 32 2
2048 486 97 4
2048 1458 269 11
2048 4374 780 30
2048 13122 2281 92
2048 39366 8472 1110

Firefox

loop size spread concat
1048576 2 217 386
524288 4 206 279
262144 8 197 228
131072 16 162 193
65536 32 169 168
32768 64 223 203
16384 128 301 317
8192 256 259 343
4096 512 227 292
2048 1024 212 251

loop数固定、配列長巨大化

loop size spread concat
2048 2 5 4
2048 6 2 1
2048 18 3 5
2048 54 7 8
2048 162 28 59
2048 486 75 109
2048 1458 281 294
2048 4374 1064 1051
2048 13122 2508 2859
2048 39366 6715 8557

傾向考察

Chromeの場合、超小型の配列に対してはspread構文が断然優位。配列が巨大になってくるとspread構文が若干不利っぽい。
しかし複数の配列を連結させる場合は、配列の長短にかかわらずspread構文は非常に遅くなるっぽい。
Firefoxの場合、Chromeとは対照的な結果。

5
2
2

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
5
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?