-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbenchmark.js
71 lines (59 loc) · 2.43 KB
/
benchmark.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/env node
'use strict';
/*
This benchmark has been copied from the intl-messageformat-parser project
for the purpose of comparing performance between two similar libraries.
While the code below has been slightly reformatted, and changed to import
our own parser rather than intl-messageformat-parser, it still strongly
resembles the code of intl-messageformat-parser's benchmark script.
intl-messageformat-parser is copyright 2019 Oath Inc. and licensed under
the New BSD License. For more, please see the original source at:
https://github.com/formatjs/formatjs/tree/master/packages/intl-messageformat-parser
*/
/* eslint-disable no-console */
/* globals console */
const benchmark = require('benchmark');
const Parser = require('./lib/parser'),
parser = new Parser;
const complex_msg = `
{gender_of_host, select,
female {
{num_guests, plural, offset:1
=0 {{host} does not give a party.}
=1 {{host} invites {guest} to her party.}
=2 {{host} invites {guest} and one other person to her party.}
other {{host} invites {guest} and # other people to her party.}
}
}
male {
{num_guests, plural, offset:1
=0 {{host} does not give a party.}
=1 {{host} invites {guest} to his party.}
=2 {{host} invites {guest} and one other person to his party.}
other {{host} invites {guest} and # other people to his party.}
}
}
other {
{num_guests, plural, offset:1
=0 {{host} does not give a party.}
=1 {{host} invites {guest} to their party.}
=2 {{host} invites {guest} and one other person to their party.}
other {{host} invites {guest} and # other people to their party.}
}
}
}
`;
const normal_msg = `Yo, {firstName} {lastName} has {numBooks, number, integer} {numBooks, plural, one {book} other {books}}.`;
const simple_msg = `Hello, {name}!`;
const string_msg = `Hello, world!`;
console.log('complex_msg AST length', JSON.stringify(parser.parse(complex_msg)).length)
console.log('normal_msg AST length', JSON.stringify(parser.parse(normal_msg)).length)
console.log('simple_msg AST length', JSON.stringify(parser.parse(simple_msg)).length)
console.log('string_msg AST length', JSON.stringify(parser.parse(string_msg)).length)
new benchmark.Suite()
.add('complex_msg', () => parser.parse(complex_msg))
.add('normal_msg', () => parser.parse(normal_msg))
.add('simple_msg', () => parser.parse(simple_msg))
.add('string_msg', () => parser.parse(string_msg))
.on('cycle', event => console.log(String(event.target)))
.run();