|
| 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() |
0 commit comments