Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
benchmark: rest operator benchmark
Benchmark comparing `util._extend()`, `Object.assign()`,
and the rest operator for object assignment.

`util._extend()` still wins currently.
  • Loading branch information
jasnell committed Feb 1, 2018
commit 8d947fa3ee39630c8da3e5fdc70632998fe76146
47 changes: 47 additions & 0 deletions benchmark/es/rest-assign.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
'use strict';

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

const bench = common.createBenchmark(main, {
method: ['rest', 'assign', '_extend'],

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_extend -> extend, otherwise Error: Unexpected method

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;
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 'rest':
bench.start();
for (i = 0; i < n; i++)
obj = { ...src };
bench.end(n);
break;
default:
throw new Error('Unexpected method');
}
}