Last active
May 8, 2021 06:37
-
-
Save beaugunderson/37f672513918bb844459 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var Benchmark = require('benchmark'); | |
var emojis = require('emojilib'); | |
var nearley = require('nearley'); | |
var emojiGrammar = require('./parsers/emoji.js'); | |
var pegjs = require('pegjs-import'); | |
var pegjsEmoji = pegjs.buildParser('./parsers/emoji.pegjs'); | |
var _ = require('lodash'); | |
function nearleyParse(string) { | |
var nearleyEmoji = new nearley.Parser(emojiGrammar.ParserRules, | |
emojiGrammar.ParserStart); | |
nearleyEmoji.feed(string); | |
return nearleyEmoji.results; | |
} | |
var characters = _(emojis) | |
.filter(function (emoji) { | |
return emoji.char; | |
}) | |
.map(function (emoji) { | |
return emoji.char; | |
}) | |
.value(); | |
var suite = new Benchmark.Suite; | |
suite | |
.add('PegJS', function () { | |
characters.forEach(function (emoji) { | |
pegjsEmoji.parse(emoji); | |
}); | |
}) | |
.add('nearley.js', function () { | |
characters.forEach(function (emoji) { | |
nearleyParse(emoji); | |
}); | |
}) | |
.on('cycle', function (event) { | |
console.log(String(event.target)); | |
}) | |
.on('complete', function () { | |
console.log('Fastest is ' + this.filter('fastest').pluck('name')); | |
}) | |
.run(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Emoji | |
-> PhoneButton {% id %} | |
| EmojiCharacter VariationSelector:? {% | |
function (data, location, reject) { | |
var e = data[0]; | |
var v = data[1]; | |
return (e.join ? e.join('') : e) + (v ? v.join ? v.join('') : v : ''); | |
} | |
%} | |
PhoneButton | |
-> [0-9#*] VariationSelector:? [\u20E3] {% | |
function (data, location, reject) { | |
return data.join(''); | |
} | |
%} | |
EmojiCharacter | |
-> [\uD83C] [\uDDE6-\uDDFF] [\uD83C] [\uDDE6-\uDDFF] | |
| [\uD83C] [\uDC04-\uDFFF] | |
| [\uD83D] [\uDC00-\uDE4F] | |
| [\uD83D] [\uDE80-\uDEC5] | |
| [\u203C-\u3299] | |
| [\u24C2] | |
| [\u2702-\u27B0] | |
| [\u00A9\u00AE] | |
VariationSelector | |
-> [\uFE0E\uFE0F] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Emoji | |
= p:PhoneButton | |
/ e:EmojiCharacter v:VariationSelector? { | |
return (e.join ? e.join('') : e) + (v ? v.join ? v.join('') : v : '') | |
} | |
PhoneButton | |
= [0-9#*] VariationSelector? '\u20E3' {return text()} | |
EmojiCharacter | |
= [\uD83C][\uDDE6-\uDDFF][\uD83C][\uDDE6-\uDDFF] | |
/ [\uD83C][\uDC04-\uDFFF] | |
/ [\uD83D][\uDC00-\uDE4F] | |
/ [\uD83D][\uDE80-\uDEC5] | |
/ [\u203C-\u3299] | |
/ '\u24C2' | |
/ [\u2702-\u27B0] | |
/ [\u00A9\u00AE] | |
VariationSelector | |
= [\uFE0E\uFE0F] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
PegJS x 62.04 ops/sec ±3.87% (67 runs sampled) | |
nearley.js x 30.54 ops/sec ±2.06% (55 runs sampled) | |
Fastest is PegJS |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment