Skip to content

Commit

Permalink
benchmark: (es) refactor
Browse files Browse the repository at this point in the history
PR-URL: nodejs#18320
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
BridgeAR authored and ryzokuken committed Jun 6, 2018
1 parent ecb3fd5 commit cf8a4af
Show file tree
Hide file tree
Showing 9 changed files with 131 additions and 120 deletions.
28 changes: 11 additions & 17 deletions benchmark/es/defaultparams-bench.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,37 +20,31 @@ function defaultParams(x = 1, y = 2) {
assert.strictEqual(y, 2);
}

function runOldStyleDefaults(n) {

var i = 0;
function runOldStyleDefaults(millions) {
bench.start();
for (; i < n; i++)
for (var i = 0; i < millions * 1e6; i++)
oldStyleDefaults();
bench.end(n / 1e6);
bench.end(millions);
}

function runDefaultParams(n) {

var i = 0;
function runDefaultParams(millions) {
bench.start();
for (; i < n; i++)
for (var i = 0; i < millions * 1e6; i++)
defaultParams();
bench.end(n / 1e6);
bench.end(millions);
}

function main(conf) {
const n = +conf.millions * 1e6;

switch (conf.method) {
function main({ millions, method }) {
switch (method) {
case '':
// Empty string falls through to next line as default, mostly for tests.
case 'withoutdefaults':
runOldStyleDefaults(n);
runOldStyleDefaults(millions);
break;
case 'withdefaults':
runDefaultParams(n);
runDefaultParams(millions);
break;
default:
throw new Error('Unexpected method');
throw new Error(`Unexpected method "${method}"`);
}
}
23 changes: 14 additions & 9 deletions benchmark/es/destructuring-bench.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,45 +8,50 @@ const bench = common.createBenchmark(main, {
millions: [100]
});

function runSwapManual(n) {
function runSwapManual(millions) {
var x, y, r;
bench.start();
for (var i = 0; i < n; i++) {
for (var i = 0; i < millions * 1e6; i++) {
x = 1, y = 2;
r = x;
x = y;
y = r;
assert.strictEqual(x, 2);
assert.strictEqual(y, 1);
}
bench.end(n / 1e6);
bench.end(millions);
}

function runSwapDestructured(n) {
function runSwapDestructured(millions) {
var x, y;
bench.start();
for (var i = 0; i < n; i++) {
for (var i = 0; i < millions * 1e6; i++) {
x = 1, y = 2;
[x, y] = [y, x];
assert.strictEqual(x, 2);
assert.strictEqual(y, 1);
}
bench.end(n / 1e6);
bench.end(millions);
}

<<<<<<< HEAD
function main(conf) {
const n = +conf.millions * 1e6;

switch (conf.method) {
=======
function main({ millions, method }) {
switch (method) {
>>>>>>> 2072f343ad... benchmark: (es) refactor
case '':
// Empty string falls through to next line as default, mostly for tests.
case 'swap':
runSwapManual(n);
runSwapManual(millions);
break;
case 'destructure':
runSwapDestructured(n);
runSwapDestructured(millions);
break;
default:
throw new Error('Unexpected method');
throw new Error(`Unexpected method "${method}"`);
}
}
23 changes: 14 additions & 9 deletions benchmark/es/destructuring-object-bench.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,45 +7,50 @@ const bench = common.createBenchmark(main, {
millions: [100]
});

function runNormal(n) {
function runNormal(millions) {
var i = 0;
const o = { x: 0, y: 1 };
bench.start();
for (; i < n; i++) {
for (; i < millions * 1e6; i++) {
/* eslint-disable no-unused-vars */
const x = o.x;
const y = o.y;
const r = o.r || 2;
/* eslint-enable no-unused-vars */
}
bench.end(n / 1e6);
bench.end(millions);
}

function runDestructured(n) {
function runDestructured(millions) {
var i = 0;
const o = { x: 0, y: 1 };
bench.start();
for (; i < n; i++) {
for (; i < millions * 1e6; i++) {
/* eslint-disable no-unused-vars */
const { x, y, r = 2 } = o;
/* eslint-enable no-unused-vars */
}
bench.end(n / 1e6);
bench.end(millions);
}

<<<<<<< HEAD
function main(conf) {
const n = +conf.millions * 1e6;

switch (conf.method) {
=======
function main({ millions, method }) {
switch (method) {
>>>>>>> 2072f343ad... benchmark: (es) refactor
case '':
// Empty string falls through to next line as default, mostly for tests.
case 'normal':
runNormal(n);
runNormal(millions);
break;
case 'destructureObject':
runDestructured(n);
runDestructured(millions);
break;
default:
throw new Error('Unexpected method');
throw new Error(`Unexpected method "${method}"`);
}
}
46 changes: 23 additions & 23 deletions benchmark/es/foreach-bench.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,58 +8,58 @@ const bench = common.createBenchmark(main, {
millions: [5]
});

function useFor(n, items, count) {
var i, j;
function useFor(millions, items, count) {
bench.start();
for (i = 0; i < n; i++) {
for (j = 0; j < count; j++) {
for (var i = 0; i < millions * 1e6; i++) {
for (var j = 0; j < count; j++) {
/* eslint-disable no-unused-vars */
const item = items[j];
/* esline-enable no-unused-vars */
}
}
bench.end(n / 1e6);
bench.end(millions);
}

function useForOf(n, items) {
var i, item;
function useForOf(millions, items) {
var item;
bench.start();
for (i = 0; i < n; i++) {
for (var i = 0; i < millions * 1e6; i++) {
for (item of items) {}
}
bench.end(n / 1e6);
bench.end(millions);
}

function useForIn(n, items) {
var i, j, item;
function useForIn(millions, items) {
bench.start();
for (i = 0; i < n; i++) {
for (j in items) {
for (var i = 0; i < millions * 1e6; i++) {
for (var j in items) {
/* eslint-disable no-unused-vars */
item = items[j];
const item = items[j];
/* esline-enable no-unused-vars */
}
}
bench.end(n / 1e6);
bench.end(millions);
}

function useForEach(n, items) {
var i;
function useForEach(millions, items) {
bench.start();
for (i = 0; i < n; i++) {
for (var i = 0; i < millions * 1e6; i++) {
items.forEach((item) => {});
}
bench.end(n / 1e6);
bench.end(millions);
}

<<<<<<< HEAD
function main(conf) {
const n = +conf.millions * 1e6;
const count = +conf.count;

=======
function main({ millions, count, method }) {
>>>>>>> 2072f343ad... benchmark: (es) refactor
const items = new Array(count);
var i;
var fn;
for (i = 0; i < count; i++)
for (var i = 0; i < count; i++)
items[i] = i;

switch (conf.method) {
Expand All @@ -78,7 +78,7 @@ function main(conf) {
fn = useForEach;
break;
default:
throw new Error('Unexpected method');
throw new Error(`Unexpected method "${method}"`);
}
fn(n, items, count);
fn(millions, items, count);
}
Loading

0 comments on commit cf8a4af

Please sign in to comment.