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