Skip to content

Commit

Permalink
Add benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
Shreyas Minocha committed Jan 18, 2019
1 parent dd25514 commit 154195e
Show file tree
Hide file tree
Showing 4 changed files with 276 additions and 23 deletions.
10 changes: 6 additions & 4 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,18 @@
"max-len": [2, 255, 4],
"no-param-reassign": 0,
"spaced-comment": [2, "always", { "markers": ["/", "!"] }],
"no-plusplus": 0
"no-plusplus": 0,
"no-console": 0,
"import/no-extraneous-dependencies": [2, { "devDependencies": true }],
},
"env": {
"amd": true,
"node": true,
"commonjs": true,
"es6": true,
"browser": true
"browser": true,
},
"globals": {
"VerEx": true
}
"VerEx": true,
},
}
136 changes: 136 additions & 0 deletions benchmark/run.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
const { Suite } = require('benchmark');
const { table } = require('table');
const prettyMs = require('pretty-ms');
const { bold } = require('chalk');
const VerEx = require('../dist/verbalexpressions');

const benchmarks = {};

benchmarks.constructor = {
VerEx: () => { VerEx(); },
RegExp: () => { RegExp(); },
};

benchmarks.startOfLine = {
VerEx: () => { VerEx().startOfLine('a').test('alpha'); },
RegExp: () => { /^a/.test('alpha'); },
};

benchmarks.endOfLine = {
VerEx: () => { VerEx().endOfLine('a').test('alpha'); },
RegExp: () => { /a$/.test('alpha'); },
};

benchmarks['then|find'] = {
VerEx: () => { VerEx().find('alpha').test('alpha'); },
RegExp: () => { /alpha/.test('alpha'); },
};

benchmarks.maybe = {
VerEx: () => {
const expr = VerEx().maybe('a');
expr.test('a'); expr.test('');
},
RegExp: () => {
const expr = /a?/;
expr.test('a'); expr.test('');
},
};

benchmarks.anything = {
VerEx: () => { VerEx().anything().test('alpha'); },
RegExp: () => { /.*/.test('alpha'); },
};

benchmarks.anythingBut = {
VerEx: () => {
const expr = VerEx().anythingBut('a');
expr.test('alpha'); expr.test('epsilon');
},
RegExp: () => {
const expr = /.*/;
expr.test('alpha'); expr.test('epsilon');
},
};

benchmarks.something = {
VerEx: () => {
const expr = VerEx().somethingBut('a');
expr.test('alpha'); expr.test('');
},
RegExp: () => {
const expr = /.+/;
expr.test('alpha'); expr.test('');
},
};

benchmarks.somethingBut = {
VerEx: () => {
const expr = VerEx().somethingBut('a');
expr.test('alpha'); expr.test('epsilon'); expr.test('');
},
RegExp: () => {
const expr = /.+/;
expr.test('alpha'); expr.test('epsilon'); expr.test('');
},
};

/*
* TODO
*/

// benchmarks['anyOf|any'] = { VerEx: () => {}, RegExp: () => {} };
// benchmarks.not = { VerEx: () => {}, RegExp: () => {} };
// benchmarks.range = { VerEx: () => {}, RegExp: () => {} };
// benchmarks['lineBreak|br'] = { VerEx: () => {}, RegExp: () => {} };
// benchmarks.tab = { VerEx: () => {}, RegExp: () => {} };
// benchmarks.word = { VerEx: () => {}, RegExp: () => {} };
// benchmarks.digit = { VerEx: () => {}, RegExp: () => {} };
// benchmarks.whitespace = { VerEx: () => {}, RegExp: () => {} };
// benchmarks.addModifier = { VerEx: () => {}, RegExp: () => {} };
// benchmarks.removeModifier = { VerEx: () => {}, RegExp: () => {} };
// benchmarks.withAnyCase = { VerEx: () => {}, RegExp: () => {} };
// benchmarks.stopAtFirst = { VerEx: () => {}, RegExp: () => {} };
// benchmarks.searchOneLine = { VerEx: () => {}, RegExp: () => {} };
// benchmarks.repeatPrevious = { VerEx: () => {}, RegExp: () => {} };
// benchmarks.oneOrMore = { VerEx: () => {}, RegExp: () => {} };
// benchmarks.multiple = { VerEx: () => {}, RegExp: () => {} };
// benchmarks.beginCapture = { VerEx: () => {}, RegExp: () => {} };
// benchmarks.endCapture = { VerEx: () => {}, RegExp: () => {} };
// benchmarks.replace = { VerEx: () => {}, RegExp: () => {} };
// benchmarks.toRegExp = { VerEx: () => {}, RegExp: () => {} };

const suite = new Suite();

for (const [name, bench] of Object.entries(benchmarks)) {
const VerExRun = bench.VerEx;
const RegExpRun = bench.RegExp;

suite
.add(`VerEx ${name}`, VerExRun)
.add(`RegExp ${name}`, RegExpRun);
}

const tableData = [];

tableData.push([
bold('Description'), bold('Time'), bold('Uncertainty'),
]);

suite.on('cycle', (event) => {
const { name, stats } = event.target;

tableData.push([
name,
prettyMs(stats.mean * 1000, { formatSubMs: true }),
${stats.rme.toFixed(2)}%`,
]);
});

suite.run();

const tableConfig = {
drawHorizontalLine: index => index === 0 || index % 2 !== 0,
};

console.log(table(tableData, tableConfig));
147 changes: 129 additions & 18 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 154195e

Please sign in to comment.