spread構文とapply関数の速度比較をしてみます。
for(let loop=1e7,size=10,n=4,fn=a=>a;n--;loop/=10,size*=10){
let A=Array(size).fill(1),t=new Date;
for(let a=loop;a--;)fn.apply(0,A);
t=new Date-t;
let u=new Date;
for(let a=loop;a--;)fn(...A);
console.log("loop:",loop,"size:",size,"time:",t,new Date-u)
}
結果
[Google Chrome 128.0.6613.113]
loop: 10000000 size: 10 time: 820 545
loop: 1000000 size: 100 time: 263 222
loop: 100000 size: 1000 time: 184 174
loop: 10000 size: 10000 time: 175 170
loop: 1000 size: 100000 time: 181 173
[Edge 128.0.2739.54]
loop: 10000000 size: 10 time: 834 573
loop: 1000000 size: 100 time: 270 234
loop: 100000 size: 1000 time: 201 177
loop: 10000 size: 10000 time: 181 173
[Fire Fox 97.0.1]
loop: 10000000 size: 10 time: 313 3946
loop: 1000000 size: 50 time: 83 1675
loop: 100000 size: 250 time: 19 938
loop: 10000 size: 1250 time: 63 3478
loop: 1000 size: 6250 time: 29 1756
[Fire Fox 97.0.1]
loop: 10000000 size: 10 time: 320 3897
loop: 1000000 size: 80 time: 119 2479
loop: 100000 size: 640 time: 361 17878
loop: 10000 size: 5120 time: 254 14776
loop: 1000 size: 40960 time: 570 12094
GCとEdgeの場合は僅差でspread構文の方が高速、FFの場合はぶっちぎりでapply関数が高速。ちなみにFFではsize*=5
とsize*=8
で検証(低速過ぎるので)。
それにしてもFFのloop: 10000 size: 1250 time: 63 3478
の結果が低速なのは疑問
Object
ついでなのでObject.assign
とも比較
{const a={},z={},loop=1e3;
for(let b=1296,c;a[c=(b*b).toString(36)]=z[c+"_"]=b--;);
//単体
let t=new Date;
for(let b=loop,c;b--;)c={...a};
console.log("spread",new Date-t);
t=new Date;
for(let b=loop,c;b--;)Object.assign(c={},a);
console.log("assign",new Date-t)
//複体
t=new Date;
for(let b=loop,c;b--;)c={...a,...z};
console.log("spread x2",new Date-t);
t=new Date;
for(let b=loop,c;b--;Object.assign(c,z))Object.assign(c={},a);
console.log("assign x2",new Date-t)
}
結果
[Google Chrome 128.0.6613.113]
spread 831
assign 903
spread x2 1687
assign x2 1811
[Edge 128.0.2739.54]
spread 886
assign 929
spread x2 1828
assign x2 1939
[FireFox 97.0.1]
spread 829
assign 589
spread x2 1306
assign x2 1070
これもFFは関数の方が高速で、FF以外はspreadの方が高速という結果