Skip to content

Instantly share code, notes, and snippets.

@hildjj
Created September 6, 2022 02:16
Show Gist options
  • Save hildjj/329861ff938140f2d7234647bd56a0f7 to your computer and use it in GitHub Desktop.
Save hildjj/329861ff938140f2d7234647bd56a0f7 to your computer and use it in GitHub Desktop.
editorconfig parser comparison
/* eslint-disable no-console */
import { Benchmark } from 'kelonio'
import { parse } from './iniFile'
import { parse_to_json } from './one-ini/editorconfig_ini'
import { promises as fs } from 'fs'
import * as path from 'path'
interface WasmParseResults {
version: string
body?: (WasmPair | WasmComment | WasmSection)[]
}
interface WasmSection {
type: 'Section'
name: string
body?: (WasmPair | WasmComment)[]
}
interface WasmPair {
type: 'Pair'
key: string
value: string
}
interface WasmComment {
type: 'Comment'
indicator: ';' | '#'
value: string
}
export type SectionName = string | null
export interface SectionBody { [key: string]: string }
export type ParseStringResult = [SectionName, SectionBody][]
const FILES = [
'tests/filetree/parent_directory.in',
'tests/filetree/unset/unset.in',
'tests/filetree/root_mixed/root_file.in',
'tests/filetree/root_file/root_file.in',
'tests/filetree/path_with_special_[chars/path_with_special_chars.in',
'tests/filetree/unset.in',
'tests/filetree/parent_directory/parent_directory.in',
'tests/filetree/path_separator.in',
'tests/filetree/root_file.in',
'tests/meta/meta.in',
'tests/cli/cli.in',
'tests/parser/empty.in',
'tests/parser/comments_only.in',
'tests/parser/newlines_only.in',
'tests/parser/limits.in',
'tests/parser/comments_and_newlines.in',
'tests/parser/crlf.in',
'tests/parser/comments.in',
'tests/parser/whitespace.in',
'tests/parser/basic.in',
'tests/parser/bom.in',
'tests/properties/indent_size_default.in',
'tests/properties/lowercase_values.in',
'tests/properties/lowercase_names.in',
'tests/properties/tab_width_default.in',
'tests/glob/brackets.in',
'tests/glob/star.in',
'tests/glob/question.in',
'tests/glob/star_star.in',
'tests/glob/utf8char.in',
'tests/glob/braces.in',
]
async function main(): Promise<void> {
const CONFIGS = await Promise.all(FILES.map(f => fs.readFile(path.join(__dirname, '..', f), 'utf8')))
const benchmark = new Benchmark()
await benchmark.record('Peggy', () => CONFIGS.forEach((c, i) => {
parse(c, {grammarSource: FILES[i]})
}), { iterations: 10000 })
await benchmark.record('one-ini only', () => CONFIGS.forEach((data, i) => {
parse_to_json(data) as WasmParseResults
}), { iterations: 10000 })
await benchmark.record('one-ini plus remap', () => CONFIGS.forEach((data, i) => {
const w = parse_to_json(data) as WasmParseResults
const res: ParseStringResult = [[null, {}]]
if (w.body) {
for (const b of w.body) {
switch (b.type) {
case 'Pair':
res[0][1][b.key.trim()] = b.value.trim()
break
case 'Section': {
const o: SectionBody = {}
if (b.body) {
for (const bb of b.body) {
if (bb.type === 'Pair') {
o[bb.key.trim()] = bb.value.trim()
}
}
}
res.push([b.name.replace(/\\\\/g, '\\\\\\\\'), o])
break
}
default:
// Ignored
}
}
}
}), { iterations: 10000 })
console.log(benchmark.report())
}
main().catch(console.error)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment