2016-09-21 11:47:26 +08:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const common = require('../common.js');
|
|
|
|
|
|
|
|
const bench = common.createBenchmark(main, {
|
|
|
|
method: ['normal', 'destructureObject'],
|
2023-01-29 20:13:35 +02:00
|
|
|
n: [1e8],
|
2016-09-21 11:47:26 +08:00
|
|
|
});
|
|
|
|
|
2018-03-17 20:49:09 +05:30
|
|
|
function runNormal(n) {
|
2017-09-13 22:48:53 -03:00
|
|
|
const o = { x: 0, y: 1 };
|
2016-09-21 11:47:26 +08:00
|
|
|
bench.start();
|
2020-01-24 18:12:49 +01:00
|
|
|
for (let i = 0; i < n; i++) {
|
2016-09-21 11:47:26 +08:00
|
|
|
/* eslint-disable no-unused-vars */
|
2017-09-13 22:48:53 -03:00
|
|
|
const x = o.x;
|
|
|
|
const y = o.y;
|
|
|
|
const r = o.r || 2;
|
2016-09-21 11:47:26 +08:00
|
|
|
/* eslint-enable no-unused-vars */
|
|
|
|
}
|
2018-03-17 20:49:09 +05:30
|
|
|
bench.end(n);
|
2016-09-21 11:47:26 +08:00
|
|
|
}
|
|
|
|
|
2018-03-17 20:49:09 +05:30
|
|
|
function runDestructured(n) {
|
2017-09-13 22:48:53 -03:00
|
|
|
const o = { x: 0, y: 1 };
|
2016-09-21 11:47:26 +08:00
|
|
|
bench.start();
|
2020-01-24 18:12:49 +01:00
|
|
|
for (let i = 0; i < n; i++) {
|
2016-09-21 11:47:26 +08:00
|
|
|
/* eslint-disable no-unused-vars */
|
2017-09-13 22:48:53 -03:00
|
|
|
const { x, y, r = 2 } = o;
|
2016-09-21 11:47:26 +08:00
|
|
|
/* eslint-enable no-unused-vars */
|
|
|
|
}
|
2018-03-17 20:49:09 +05:30
|
|
|
bench.end(n);
|
2016-09-21 11:47:26 +08:00
|
|
|
}
|
|
|
|
|
2018-03-17 20:49:09 +05:30
|
|
|
function main({ n, method }) {
|
2017-12-30 03:59:27 +01:00
|
|
|
switch (method) {
|
2016-09-21 11:47:26 +08:00
|
|
|
case 'normal':
|
2018-03-17 20:49:09 +05:30
|
|
|
runNormal(n);
|
2016-09-21 11:47:26 +08:00
|
|
|
break;
|
|
|
|
case 'destructureObject':
|
2018-03-17 20:49:09 +05:30
|
|
|
runDestructured(n);
|
2016-09-21 11:47:26 +08:00
|
|
|
break;
|
|
|
|
default:
|
2018-01-23 13:18:30 +01:00
|
|
|
throw new Error(`Unexpected method "${method}"`);
|
2016-09-21 11:47:26 +08:00
|
|
|
}
|
|
|
|
}
|