Skip to content

Commit fe36d55

Browse files
committed
chore: Add Myna parser to the performance comparison
1 parent 8c85dec commit fe36d55

File tree

5 files changed

+56
-0
lines changed

5 files changed

+56
-0
lines changed

benchmarks/myna/pure.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Implements a JSON (JavaScript Object Notation) grammar using the Myna parsing library
2+
// See http://www.json.org
3+
function CreateJsonGrammar (myna) {
4+
// Setup a shorthand for the Myna parsing library object
5+
let m = myna
6+
7+
let g = new function() {
8+
// These are helper rules, they do not create nodes in the parse tree.
9+
this.escapedChar = m.seq('\\', m.advance)
10+
this.quoteChar = m.choice(this.escapedChar, m.notChar('"'))
11+
this.fraction = m.seq(".", m.digit.zeroOrMore)
12+
this.plusOrMinus = m.char("+-")
13+
this.exponent = m.seq(m.char("eE"), this.plusOrMinus.opt, m.digits)
14+
this.comma = m.char(",").ws
15+
16+
// The following rules create nodes in the abstract syntax tree
17+
this.string = m.doubleQuoted(this.quoteChar.zeroOrMore).ast
18+
this.null = m.keyword("null").ast
19+
this.bool = m.keywords("true", "false").ast
20+
this.number = m.seq(this.plusOrMinus.opt, m.integer, this.fraction.opt, this.exponent.opt).ast
21+
22+
let _this = this
23+
this.value = m.choice(this.string, this.bool, this.null, this.number,
24+
// Using a lazy evaluation rule to allow recursive rule definitions
25+
m.delay(function() { return m.choice(_this.object, _this.array)
26+
}))
27+
28+
this.array = m.bracketed(m.delimited(this.value.ws, this.comma)).ast
29+
this.pair = m.seq(this.string, m.ws, ":", m.ws, this.value.ws).ast
30+
this.object = m.braced(m.delimited(this.pair.ws, this.comma)).ast
31+
}
32+
33+
// Register the grammar, providing a name and the default parse rule
34+
return m.registerGrammar("json", g, g.object)
35+
}
36+
37+
// Export the grammar for usage by Node.js and CommonJs compatible module loaders
38+
if (typeof module === "object" && module.exports) {
39+
module.exports = CreateJsonGrammar
40+
}

benchmarks/package-lock.json

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

benchmarks/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"jison": "0.4.18",
1212
"json-to-ast": "2.1.0",
1313
"json5": "2.1.1",
14+
"myna-parser": "2.5.1",
1415
"pegjs": "0.10.0"
1516
}
1617
}

benchmarks/parse.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ const astParse = require('json-to-ast')
1717
const JSON5 = require('json5')
1818
const { parse: parseWithComments } = require('comment-json')
1919
const { parse: parseThis, tokenize: tokenizeThis } = require('..')
20+
const myna = require('myna-parser')
21+
require('./myna/pure')(myna)
22+
const mynaParse = myna.parsers.json
2023

2124
const pkg = require('../package')
2225
const input = JSON.stringify(pkg, undefined, 2)
@@ -102,6 +105,10 @@ function parseAST () {
102105
})
103106
}
104107

108+
function parseMyna () {
109+
mynaParse(input)
110+
}
111+
105112
createSuite(`Parsing JSON data ${input.length} characters long using`)
106113
.add('the built-in parser', parseBuiltIn)
107114
.add('the pure chevrotain parser', parsePureChevrotain)
@@ -112,6 +119,7 @@ createSuite(`Parsing JSON data ${input.length} characters long using`)
112119
.add('the pure hand-built parser', parseHandbuilt)
113120
.add('the extended hand-built parser', parseExtendedHandbuilt)
114121
.add('the AST parser', parseAST)
122+
.add('the Myna parser', parseMyna)
115123
.add('the pure jju parser', parsePureJju)
116124
.add('the extended jju parser', parseExtendedJju)
117125
.add('the tokenisable jju parser', parseTokenisableJju)

benchmarks/results/performance.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ This is a result of the [benchmark] run by `npm run benchmarks`. The numbers sho
3737
the pure hand-built parser x 7,508 ops/sec ±0.49% (86 runs sampled)
3838
the extended hand-built parser x 7,517 ops/sec ±0.45% (90 runs sampled)
3939
the AST parser x 8,008 ops/sec ±0.90% (85 runs sampled)
40+
the Myna parser x 3,124 ops/sec ±0.66% (90 runs sampled)
4041
the pure jju parser x 7,505 ops/sec ±0.64% (89 runs sampled)
4142
the extended jju parser x 7,352 ops/sec ±0.45% (90 runs sampled)
4243
the tokenisable jju parser x 6,636 ops/sec ±0.46% (89 runs sampled)

0 commit comments

Comments
 (0)