Skip to content
This repository was archived by the owner on Apr 10, 2023. It is now read-only.

Commit 7c2dbc5

Browse files
committed
feat(benchmark): ✨Add
1 parent 32eb57f commit 7c2dbc5

File tree

4 files changed

+111
-0
lines changed

4 files changed

+111
-0
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,10 @@ JavaScript parser, executor and serializer.
2020
[standard-version-url]: https://github.com/conventional-changelog/standard-version
2121
[codecov-image]: https://img.shields.io/codecov/c/github/vivaxy/JavaScript.svg?style=flat-square
2222
[codecov-url]: https://codecov.io/gh/vivaxy/JavaScript
23+
24+
## Benchmark
25+
26+
@vivaxy/javascript#parse x 3,908 ops/sec ±1.64% (90 runs sampled)
27+
@babel/parser#parse x 1,967 ops/sec ±3.13% (86 runs sampled)
28+
acorn#parse x 1,200 ops/sec ±7.21% (88 runs sampled)
29+
Fastest is @vivaxy/javascript#parse

benchmark/parse.js

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/**
2+
* @since 20180828 13:34
3+
* @author vivaxy
4+
*/
5+
6+
const path = require('path');
7+
const Benchmark = require('benchmark');
8+
const fse = require('fs-extra');
9+
const glob = require('fast-glob');
10+
11+
const parse = require('../lib/parse.js');
12+
const { parse: babelParse } = require('@babel/parser');
13+
const { parse: acornParse } = require('acorn');
14+
15+
const suite = new Benchmark.Suite();
16+
17+
(async () => {
18+
const dirs = ['execute', 'parse', 'serialize'];
19+
const baseDir = path.join(__dirname, '..', 'lib', '__tests__', 'fixtures');
20+
const getTestCaseTasks = dirs.map((dir) => {
21+
const task = async () => {
22+
const testCases = await glob('*', {
23+
cwd: path.join(baseDir, dir),
24+
onlyDirectories: true,
25+
});
26+
return testCases.map((testCase) => {
27+
return path.join(dir, testCase);
28+
});
29+
};
30+
return task();
31+
});
32+
const testCaseNames = (await Promise.all(getTestCaseTasks)).reduce(
33+
(arr, list) => {
34+
return arr.concat(list);
35+
},
36+
[]
37+
);
38+
const getTestCaseInputTasks = testCaseNames.map((testCaseName) => {
39+
const task = async () => {
40+
return fse.readFile(path.join(baseDir, testCaseName, 'input.js'), 'utf8');
41+
};
42+
return task();
43+
});
44+
const testCaseInputs = await Promise.all(getTestCaseInputTasks);
45+
46+
let message = '';
47+
48+
suite
49+
.add('@vivaxy/javascript#parse', function() {
50+
testCaseInputs.forEach((input) => {
51+
parse(input);
52+
});
53+
})
54+
.add('@babel/parser#parse', function() {
55+
testCaseInputs.forEach((input) => {
56+
babelParse(input);
57+
});
58+
})
59+
.add('acorn#parse', function() {
60+
testCaseInputs.forEach((input) => {
61+
acornParse(input);
62+
});
63+
})
64+
.on('cycle', function(event) {
65+
message += String(event.target) + '\n';
66+
})
67+
.on('complete', async function() {
68+
message += 'Fastest is ' + this.filter('fastest').map('name');
69+
70+
const readmePath = path.join(__dirname, '..', 'README.md');
71+
const splitString = '\n## ';
72+
const readme = await fse.readFile(readmePath, 'utf8');
73+
const readmes = readme.split(splitString).map((readmeSection) => {
74+
if (readmeSection.startsWith('Benchmark')) {
75+
return 'Benchmark\n\n' + message + '\n';
76+
}
77+
return readmeSection;
78+
});
79+
await fse.writeFile(readmePath, readmes.join(splitString));
80+
})
81+
.run();
82+
})();

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@
2020
},
2121
"homepage": "https://github.com/vivaxy/JavaScript#readme",
2222
"devDependencies": {
23+
"@babel/parser": "^7.0.0",
24+
"acorn": "^5.7.2",
2325
"ava": "^0.25.0",
26+
"benchmark": "^2.1.4",
2427
"fast-glob": "^2.2.2",
2528
"fs-extra": "^7.0.0",
2629
"husky": "^0.14.3",

yarn.lock

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@
8686
version "7.0.0-beta.51"
8787
resolved "https://registry.npmjs.org/@babel/parser/-/parser-7.0.0-beta.51.tgz#27cec2df409df60af58270ed8f6aa55409ea86f6"
8888

89+
"@babel/parser@^7.0.0":
90+
version "7.0.0"
91+
resolved "https://registry.npmjs.org/@babel/parser/-/parser-7.0.0.tgz#697655183394facffb063437ddf52c0277698775"
92+
8993
"@babel/template@7.0.0-beta.51":
9094
version "7.0.0-beta.51"
9195
resolved "https://registry.npmjs.org/@babel/template/-/template-7.0.0-beta.51.tgz#9602a40aebcf357ae9677e2532ef5fc810f5fbff"
@@ -161,6 +165,10 @@ abbrev@1:
161165
version "1.1.1"
162166
resolved "https://registry.yarnpkg.com/abbrev/-/abbrev-1.1.1.tgz#f8f2c887ad10bf67f634f005b6987fed3179aac8"
163167

168+
acorn@^5.7.2:
169+
version "5.7.2"
170+
resolved "https://registry.npmjs.org/acorn/-/acorn-5.7.2.tgz#91fa871883485d06708800318404e72bfb26dcc5"
171+
164172
align-text@^0.1.1, align-text@^0.1.3:
165173
version "0.1.4"
166174
resolved "https://registry.yarnpkg.com/align-text/-/align-text-0.1.4.tgz#0cd90a561093f35d0a99256c22b7069433fad117"
@@ -735,6 +743,13 @@ base@^0.11.1:
735743
mixin-deep "^1.2.0"
736744
pascalcase "^0.1.1"
737745

746+
benchmark@^2.1.4:
747+
version "2.1.4"
748+
resolved "https://registry.npmjs.org/benchmark/-/benchmark-2.1.4.tgz#09f3de31c916425d498cc2ee565a0ebf3c2a5629"
749+
dependencies:
750+
lodash "^4.17.4"
751+
platform "^1.3.3"
752+
738753
binary-extensions@^1.0.0:
739754
version "1.11.0"
740755
resolved "https://registry.yarnpkg.com/binary-extensions/-/binary-extensions-1.11.0.tgz#46aa1751fb6a2f93ee5e689bb1087d4b14c6c205"
@@ -3334,6 +3349,10 @@ pkg-dir@^2.0.0:
33343349
dependencies:
33353350
find-up "^2.1.0"
33363351

3352+
platform@^1.3.3:
3353+
version "1.3.5"
3354+
resolved "https://registry.npmjs.org/platform/-/platform-1.3.5.tgz#fb6958c696e07e2918d2eeda0f0bc9448d733444"
3355+
33373356
please-upgrade-node@^3.0.2:
33383357
version "3.1.1"
33393358
resolved "https://registry.npmjs.org/please-upgrade-node/-/please-upgrade-node-3.1.1.tgz#ed320051dfcc5024fae696712c8288993595e8ac"

0 commit comments

Comments
 (0)