• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3const common = require('../common.js');
4let icu;
5try {
6  icu = common.binding('icu');
7} catch {
8  // Continue regardless of error.
9}
10const punycode = require('punycode');
11
12const bench = common.createBenchmark(main, {
13  method: ['punycode'].concat(icu !== undefined ? ['icu'] : []),
14  n: [1024],
15  val: [
16    'افغانستا.icom.museum',
17    'الجزائر.icom.museum',
18    'österreich.icom.museum',
19    'বাংলাদেশ.icom.museum',
20    'беларусь.icom.museum',
21    'belgië.icom.museum',
22    'българия.icom.museum',
23    'تشادر.icom.museum',
24    '中国.icom.museum',
25    'القمر.icom.museum',
26    'κυπρος.icom.museum',
27    'českárepublika.icom.museum',
28    'مصر.icom.museum',
29    'ελλάδα.icom.museum',
30    'magyarország.icom.museum',
31    'ísland.icom.museum',
32    'भारत.icom.museum',
33    'ايران.icom.museum',
34    'éire.icom.museum',
35    'איקו״ם.ישראל.museum',
36    '日本.icom.museum',
37    'الأردن.icom.museum',
38  ],
39});
40
41function usingPunycode(val) {
42  punycode.toUnicode(punycode.toASCII(val));
43}
44
45function usingICU(val) {
46  icu.toUnicode(icu.toASCII(val));
47}
48
49function runPunycode(n, val) {
50  for (let i = 0; i < n; i++)
51    usingPunycode(val);
52  bench.start();
53  for (let i = 0; i < n; i++)
54    usingPunycode(val);
55  bench.end(n);
56}
57
58function runICU(n, val) {
59  bench.start();
60  for (let i = 0; i < n; i++)
61    usingICU(val);
62  bench.end(n);
63}
64
65function main({ n, val, method }) {
66  switch (method) {
67    case 'punycode':
68      runPunycode(n, val);
69      break;
70    case 'icu':
71      if (icu !== undefined) {
72        runICU(n, val);
73        break;
74      }
75      // fallthrough
76    default:
77      throw new Error(`Unexpected method "${method}"`);
78  }
79}
80