Skip to content

Commit 6abb06f

Browse files
suryaghjasnell
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 <[email protected]> Reviewed-By: Brian White <[email protected]>
1 parent 3c09d1b commit 6abb06f

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
'use strict';
2+
3+
const common = require('../common.js');
4+
const util = require('util');
5+
const v8 = require('v8');
6+
7+
const bench = common.createBenchmark(main, {
8+
type: ['extend', 'assign'],
9+
n: [10e4]
10+
});
11+
12+
function main(conf) {
13+
let fn;
14+
const n = conf.n | 0;
15+
let v8command;
16+
17+
if (conf.type === 'extend') {
18+
fn = util._extend;
19+
v8command = '%OptimizeFunctionOnNextCall(util._extend)';
20+
} else if (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 (var i = 0; i < conf.type.length * 10; i += 1)
29+
fn({}, process.env);
30+
31+
v8.setFlagsFromString('--allow_natives_syntax');
32+
eval(v8command);
33+
34+
var obj = new Proxy({}, { set: function(a, b, c) { return true; } });
35+
36+
bench.start();
37+
for (var j = 0; j < n; j += 1)
38+
fn(obj, process.env);
39+
bench.end(n);
40+
}

0 commit comments

Comments
 (0)