Skip to content

Commit ffbad83

Browse files
jasnellMylesBorins
authored andcommitted
benchmark: spread operator benchmark
Benchmark comparing `util._extend()`, `Object.assign()`, and the spread operator for object assignment. `util._extend()` still wins currently. PR-URL: #18442 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Weijia Wang <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Yuta Hiroto <[email protected]>
1 parent 442903f commit ffbad83

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

‎benchmark/es/spread-assign.js‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
'use strict';
2+
3+
constcommon=require('../common.js');
4+
constutil=require('util');
5+
6+
constbench=common.createBenchmark(main,{
7+
method: ['spread','assign','_extend'],
8+
count: [5,10,20],
9+
millions: [1]
10+
});
11+
12+
functionmain({ millions, context, count, rest, method }){
13+
constn=millions*1e6;
14+
15+
constsrc={};
16+
for(letn=0;n<count;n++)
17+
src[`p${n}`]=n;
18+
19+
letobj;// eslint-disable-line
20+
leti;
21+
22+
switch(method){
23+
case'':
24+
// Empty string falls through to next line as default, mostly for tests.
25+
case'_extend':
26+
bench.start();
27+
for(i=0;i<n;i++)
28+
obj=util._extend({},src);
29+
bench.end(n);
30+
break;
31+
case'assign':
32+
bench.start();
33+
for(i=0;i<n;i++)
34+
obj=Object.assign({},src);
35+
bench.end(n);
36+
break;
37+
case'spread':
38+
bench.start();
39+
for(i=0;i<n;i++)
40+
obj={ ...src};
41+
bench.end(n);
42+
break;
43+
default:
44+
thrownewError('Unexpected method');
45+
}
46+
}

0 commit comments

Comments
(0)