• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3const common = require('../common.js');
4
5const bench = common.createBenchmark(main, {
6  method: ['normal', 'destructureObject'],
7  n: [1e8],
8});
9
10function runNormal(n) {
11  const o = { x: 0, y: 1 };
12  bench.start();
13  for (let i = 0; i < n; i++) {
14    /* eslint-disable no-unused-vars */
15    const x = o.x;
16    const y = o.y;
17    const r = o.r || 2;
18    /* eslint-enable no-unused-vars */
19  }
20  bench.end(n);
21}
22
23function runDestructured(n) {
24  const o = { x: 0, y: 1 };
25  bench.start();
26  for (let i = 0; i < n; i++) {
27    /* eslint-disable no-unused-vars */
28    const { x, y, r = 2 } = o;
29    /* eslint-enable no-unused-vars */
30  }
31  bench.end(n);
32}
33
34function main({ n, method }) {
35  switch (method) {
36    case 'normal':
37      runNormal(n);
38      break;
39    case 'destructureObject':
40      runDestructured(n);
41      break;
42    default:
43      throw new Error(`Unexpected method "${method}"`);
44  }
45}
46