Skip to content

Commit

Permalink
benchmark: (es) refactor
Browse files Browse the repository at this point in the history
PR-URL: #18320
Reviewed-By: James M Snell <[email protected]>
  • Loading branch information
BridgeAR authored and MylesBorins committed Feb 21, 2018
1 parent 76cb958 commit 076b3d9
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 129 deletions.
24 changes: 9 additions & 15 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({ millions, method }) {
const n = millions * 1e6;

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}"`);
}
}
20 changes: 9 additions & 11 deletions benchmark/es/destructuring-bench.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,45 +8,43 @@ 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);
}

function main({ millions, method }) {
const n = millions * 1e6;

switch (method) {
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}"`);
}
}
20 changes: 9 additions & 11 deletions benchmark/es/destructuring-object-bench.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,45 +7,43 @@ 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);
}

function main({ millions, method }) {
const n = millions * 1e6;

switch (method) {
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}"`);
}
}
43 changes: 19 additions & 24 deletions benchmark/es/foreach-bench.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,56 +8,51 @@ 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);
}

function main({ millions, count, method }) {
const n = millions * 1e6;
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 (method) {
Expand All @@ -76,7 +71,7 @@ function main({ millions, count, method }) {
fn = useForEach;
break;
default:
throw new Error('Unexpected method');
throw new Error(`Unexpected method "${method}"`);
}
fn(n, items, count);
fn(millions, items, count);
}
58 changes: 25 additions & 33 deletions benchmark/es/map-bench.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,63 +11,59 @@ const bench = common.createBenchmark(main, {
millions: [1]
});

function runObject(n) {
function runObject(millions) {
const m = {};
var i = 0;
bench.start();
for (; i < n; i++) {
for (var i = 0; i < millions * 1e6; i++) {
m[`i${i}`] = i;
m[`s${i}`] = String(i);
assert.strictEqual(String(m[`i${i}`]), m[`s${i}`]);
m[`i${i}`] = undefined;
m[`s${i}`] = undefined;
}
bench.end(n / 1e6);
bench.end(millions);
}

function runNullProtoObject(n) {
function runNullProtoObject(millions) {
const m = Object.create(null);
var i = 0;
bench.start();
for (; i < n; i++) {
for (var i = 0; i < millions * 1e6; i++) {
m[`i${i}`] = i;
m[`s${i}`] = String(i);
assert.strictEqual(String(m[`i${i}`]), m[`s${i}`]);
m[`i${i}`] = undefined;
m[`s${i}`] = undefined;
}
bench.end(n / 1e6);
bench.end(millions);
}

function runNullProtoLiteralObject(n) {
function runNullProtoLiteralObject(millions) {
const m = { __proto__: null };
var i = 0;
bench.start();
for (; i < n; i++) {
for (var i = 0; i < millions * 1e6; i++) {
m[`i${i}`] = i;
m[`s${i}`] = String(i);
assert.strictEqual(String(m[`i${i}`]), m[`s${i}`]);
m[`i${i}`] = undefined;
m[`s${i}`] = undefined;
}
bench.end(n / 1e6);
bench.end(millions);
}

function StorageObject() {}
StorageObject.prototype = Object.create(null);

function runStorageObject(n) {
function runStorageObject(millions) {
const m = new StorageObject();
var i = 0;
bench.start();
for (; i < n; i++) {
for (var i = 0; i < millions * 1e6; i++) {
m[`i${i}`] = i;
m[`s${i}`] = String(i);
assert.strictEqual(String(m[`i${i}`]), m[`s${i}`]);
m[`i${i}`] = undefined;
m[`s${i}`] = undefined;
}
bench.end(n / 1e6);
bench.end(millions);
}

function fakeMap() {
Expand All @@ -80,59 +76,55 @@ function fakeMap() {
};
}

function runFakeMap(n) {
function runFakeMap(millions) {
const m = fakeMap();
var i = 0;
bench.start();
for (; i < n; i++) {
for (var i = 0; i < millions * 1e6; i++) {
m.set(`i${i}`, i);
m.set(`s${i}`, String(i));
assert.strictEqual(String(m.get(`i${i}`)), m.get(`s${i}`));
m.set(`i${i}`, undefined);
m.set(`s${i}`, undefined);
}
bench.end(n / 1e6);
bench.end(millions);
}

function runMap(n) {
function runMap(millions) {
const m = new Map();
var i = 0;
bench.start();
for (; i < n; i++) {
for (var i = 0; i < millions * 1e6; i++) {
m.set(`i${i}`, i);
m.set(`s${i}`, String(i));
assert.strictEqual(String(m.get(`i${i}`)), m.get(`s${i}`));
m.set(`i${i}`, undefined);
m.set(`s${i}`, undefined);
}
bench.end(n / 1e6);
bench.end(millions);
}

function main({ millions, method }) {
const n = millions * 1e6;

switch (method) {
case '':
// Empty string falls through to next line as default, mostly for tests.
case 'object':
runObject(n);
runObject(millions);
break;
case 'nullProtoObject':
runNullProtoObject(n);
runNullProtoObject(millions);
break;
case 'nullProtoLiteralObject':
runNullProtoLiteralObject(n);
runNullProtoLiteralObject(millions);
break;
case 'storageObject':
runStorageObject(n);
runStorageObject(millions);
break;
case 'fakeMap':
runFakeMap(n);
runFakeMap(millions);
break;
case 'map':
runMap(n);
runMap(millions);
break;
default:
throw new Error('Unexpected method');
throw new Error(`Unexpected method "${method}"`);
}
}
Loading

0 comments on commit 076b3d9

Please sign in to comment.