From ffbad8350ed4b601f0f1afb54c1d33ddebd9565f Mon Sep 17 00:00:00 2001 From: James M Snell Date: Mon, 29 Jan 2018 13:58:45 -0800 Subject: [PATCH] benchmark: spread operator benchmark Benchmark comparing `util._extend()`, `Object.assign()`, and the spread operator for object assignment. `util._extend()` still wins currently. PR-URL: https://github.com/nodejs/node/pull/18442 Reviewed-By: Colin Ihrig Reviewed-By: Weijia Wang Reviewed-By: Luigi Pinca Reviewed-By: Ruben Bridgewater Reviewed-By: Yuta Hiroto --- benchmark/es/spread-assign.js | 46 +++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 benchmark/es/spread-assign.js diff --git a/benchmark/es/spread-assign.js b/benchmark/es/spread-assign.js new file mode 100644 index 00000000000000..00c634ff879501 --- /dev/null +++ b/benchmark/es/spread-assign.js @@ -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'); + } +}