Skip to content

Commit 35c70b5

Browse files
suryaghFishrock123
authored andcommitted
benchmark: util._extend vs object.assign
To copy the values of all enumerable own properties from- a source object to a target object, node still use- `util._extend`, though newer standard `Object.assign` is available. This is because `util._extend` is found to be faster than `Object.assign`. This benchmark test is to keep track of how performance compare. PR-URL: #7255 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Brian White <mscdex@mscdex.net>
1 parent e7b8400 commit 35c70b5

1 file changed

Lines changed: 40 additions & 0 deletions

File tree

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
'use strict';
2+
3+
constcommon=require('../common.js');
4+
constutil=require('util');
5+
constv8=require('v8');
6+
7+
constbench=common.createBenchmark(main,{
8+
type: ['extend','assign'],
9+
n: [10e4]
10+
});
11+
12+
functionmain(conf){
13+
letfn;
14+
constn=conf.n|0;
15+
letv8command;
16+
17+
if(conf.type==='extend'){
18+
fn=util._extend;
19+
v8command='%OptimizeFunctionOnNextCall(util._extend)';
20+
}elseif(conf.type==='assign'){
21+
fn=Object.assign;
22+
// Object.assign is built-in, cannot be optimized
23+
v8command='';
24+
}
25+
26+
// Force-optimize the method to test so that the benchmark doesn't
27+
// get disrupted by the optimizer kicking in halfway through.
28+
for(vari=0;i<conf.type.length*10;i+=1)
29+
fn({},process.env);
30+
31+
v8.setFlagsFromString('--allow_natives_syntax');
32+
eval(v8command);
33+
34+
varobj=newProxy({},{set: function(a,b,c){returntrue;}});
35+
36+
bench.start();
37+
for(varj=0;j<n;j+=1)
38+
fn(obj,process.env);
39+
bench.end(n);
40+
}

0 commit comments

Comments
 (0)