Skip to content

Commit 78fab3f

Browse files
authored
Performance improvement (#91)
* Performance improvement * fix for node 10 * fix
1 parent 891fc2a commit 78fab3f

24 files changed

+1398
-1047
lines changed

benchmark/index.ts

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// eslint-disable-next-line eslint-comments/disable-enable-pair -- ignore
2+
/* eslint-disable require-jsdoc, no-console -- ignore */
3+
import * as Benchmark from "benchmark"
4+
import fs from "fs"
5+
import { parseForESLint } from ".."
6+
import { parseForESLint as parseOld } from "../node_modules/jsonc-eslint-parser"
7+
8+
const contents = `${fs.readFileSync(
9+
require.resolve("../package.json"),
10+
"utf-8",
11+
)}// comments`
12+
13+
type Result = { name: string; hz: number }
14+
const results: Result[] = []
15+
16+
function format(hz: number): string {
17+
return (~~(hz * 100) / 100).toString().padEnd(4, " ").padStart(6, " ")
18+
}
19+
20+
function onCycle(event: { target: Result }): void {
21+
const { name, hz } = event.target
22+
results.push({ name, hz })
23+
24+
console.log(event.target.toString())
25+
}
26+
27+
function onComplete(): void {
28+
console.log("-".repeat(72))
29+
const map: Record<string, number[]> = {}
30+
for (const result of results) {
31+
const r = (map[result.name.slice(2)] ??= [])
32+
r.push(result.hz)
33+
}
34+
for (const name of Object.keys(map)) {
35+
console.log(
36+
`${name.padEnd(15)} ${format(
37+
map[name].reduce((p, a) => p + a, 0) / map[name].length,
38+
)} ops/sec`,
39+
)
40+
}
41+
for (let i = 0; i < results.length; ++i) {
42+
const result = results[i]
43+
44+
console.log(`${result.name.padEnd(15)} ${format(result.hz)} ops/sec`)
45+
}
46+
}
47+
48+
const suite = new Benchmark.Suite("benchmark", { onCycle, onComplete })
49+
50+
for (const no of [1, 2, 3]) {
51+
suite.add(`${no} new jsonc-eslint-parser`, function () {
52+
parseForESLint(contents, {
53+
loc: true,
54+
range: true,
55+
raw: true,
56+
tokens: true,
57+
comment: true,
58+
eslintVisitorKeys: true,
59+
eslintScopeManager: true,
60+
})
61+
})
62+
suite.add(`${no} old jsonc-eslint-parser`, function () {
63+
parseOld(contents, {
64+
loc: true,
65+
range: true,
66+
raw: true,
67+
tokens: true,
68+
comment: true,
69+
eslintVisitorKeys: true,
70+
eslintScopeManager: true,
71+
})
72+
})
73+
}
74+
75+
suite.run()

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
"update": "ts-node ./tools/update.ts && npm run eslint-fix && npm run test:nyc",
2323
"preversion": "npm test && npm run update && git add .",
2424
"version": "npm run eslint-fix && git add .",
25-
"update-fixtures": "ts-node ./tools/update-fixtures.ts"
25+
"update-fixtures": "ts-node ./tools/update-fixtures.ts",
26+
"benchmark": "ts-node --transpile-only benchmark/index.ts"
2627
},
2728
"repository": {
2829
"type": "git",
@@ -43,6 +44,7 @@
4344
"homepage": "https://github.com/ota-meshi/jsonc-eslint-parser#readme",
4445
"devDependencies": {
4546
"@ota-meshi/eslint-plugin": "^0.8.0",
47+
"@types/benchmark": "^2.1.0",
4648
"@types/eslint": "^7.2.0",
4749
"@types/eslint-visitor-keys": "^1.0.0",
4850
"@types/estree": "^0.0.50",
@@ -52,6 +54,7 @@
5254
"@types/semver": "^7.3.1",
5355
"@typescript-eslint/eslint-plugin": "^4.0.0-0",
5456
"@typescript-eslint/parser": "^4.0.0-0",
57+
"benchmark": "^2.1.4",
5558
"eslint": "^7.3.0",
5659
"eslint-config-prettier": "^8.0.0",
5760
"eslint-plugin-eslint-comments": "^3.2.0",
@@ -70,6 +73,7 @@
7073
"vue-eslint-parser": "^7.2.0"
7174
},
7275
"dependencies": {
76+
"acorn": "^7.4.1",
7377
"eslint-utils": "^2.1.0",
7478
"eslint-visitor-keys": "^1.3.0",
7579
"espree": "^6.0.0",

0 commit comments

Comments
 (0)