• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3const common = require('../common.js');
4const util = require('util');
5
6const bench = common.createBenchmark(main, {
7  method: ['spread', 'assign', '_extend'],
8  count: [5, 10, 20],
9  n: [1e6]
10});
11
12function main({ n, context, count, rest, method }) {
13
14  const src = {};
15  for (let n = 0; n < count; n++)
16    src[`p${n}`] = n;
17
18  let obj;
19
20  switch (method) {
21    case '_extend':
22      bench.start();
23      for (let i = 0; i < n; i++)
24        obj = util._extend({}, src);
25      bench.end(n);
26      break;
27    case 'assign':
28      bench.start();
29      for (let i = 0; i < n; i++)
30        obj = Object.assign({}, src);
31      bench.end(n);
32      break;
33    case 'spread':
34      bench.start();
35      for (let i = 0; i < n; i++)
36        obj = { ...src }; // eslint-disable-line no-unused-vars
37      bench.end(n);
38      break;
39    default:
40      throw new Error('Unexpected method');
41  }
42}
43