forked from forzagreen/n2words
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bench.js
68 lines (58 loc) · 1.64 KB
/
bench.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
/* eslint-disable node/no-unsupported-features/es-syntax */
/* eslint-disable import/no-nodejs-modules */
import Benchmark from 'benchmark';
import * as fs from 'fs';
import chalk from 'chalk';
const suite = new Benchmark.Suite();
// Parse CLI arguments
const args = process.argv;
args.slice(2);
// Default argument values
let i18n;
let value = Number.MAX_SAFE_INTEGER;
// Look for matching CLI arguments
for (let i = 1; i < args.length; i++) {
if (args[i] == '--lang' || args[i] == '--language') {
i18n = args[i + 1];
} else if (args[i] == '--value') {
value = args[i + 1];
}
}
// If language is defined
if (i18n) {
// Check if language file exists
if (fs.existsSync('./lib/i18n/' + i18n + '.js')) {
// Queue specific language benchmark
await benchLanguage(i18n);
} else {
// Log error to console
console.error(chalk.red('\ni18n language file does not exist: ' + i18n + '.js\n'));
}
} else {
// Load all files in language directory
const files = fs.readdirSync('./lib/i18n');
// Loop through files
for (let i = 0; i < files.length; i++) {
// Make sure file is JavaScript
if (files[i].includes('.js')) {
// Queue language file benchmark
await benchLanguage(files[i].replace('.js', ''));
}
}
}
// Log output to console and run queued benchmarks
suite
.on('cycle', event => {
console.log(String(event.target));
})
.run();
/**
* Queue language test file for benchmarking.
* @param {string} i18n Language identifier.
*/
async function benchLanguage(i18n) {
const { default: language } = await import('./lib/i18n/' + i18n + '.js');
suite.add(i18n, () => {
language(value);
});
}