Skip to content

Commit

Permalink
benchmark: spread operator benchmark
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
jasnell authored and MylesBorins committed Feb 20, 2018
1 parent a1e45b2 commit dfa467f
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions benchmark/es/spread-assign.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
'use strict';

const common = require('../common.js');
const util = require('util');

const bench = common.createBenchmark(main, {
method: ['spread', 'assign', '_extend'],
count: [5, 10, 20],
millions: [1]
});

function main({ millions, context, count, rest, method }) {
const n = millions * 1e6;

const src = {};
for (let n = 0; n < count; n++)
src[`p${n}`] = n;

let obj; // eslint-disable-line
let i;

switch (method) {
case '':
// Empty string falls through to next line as default, mostly for tests.
case '_extend':
bench.start();
for (i = 0; i < n; i++)
obj = util._extend({}, src);
bench.end(n);
break;
case 'assign':
bench.start();
for (i = 0; i < n; i++)
obj = Object.assign({}, src);
bench.end(n);
break;
case 'spread':
bench.start();
for (i = 0; i < n; i++)
obj = { ...src };
bench.end(n);
break;
default:
throw new Error('Unexpected method');
}
}

0 comments on commit dfa467f

Please sign in to comment.