-
Notifications
You must be signed in to change notification settings - Fork 30k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
net: use icu's punycode implementation
ICU has a punycode implementation built in. Use it instead of the javascript implementation because it's much faster. PR-URL: #7355 Reviewed-By: Trevor Norris <trev.norris@gmail.com> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
- Loading branch information
Showing
6 changed files
with
291 additions
and
12 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
'use strict'; | ||
|
||
const common = require('../common.js'); | ||
const icu = process.binding('icu'); | ||
const punycode = require('punycode'); | ||
|
||
const bench = common.createBenchmark(main, { | ||
method: ['punycode', 'icu'], | ||
n: [1024], | ||
val: [ | ||
'افغانستا.icom.museum', | ||
'الجزائر.icom.museum', | ||
'österreich.icom.museum', | ||
'বাংলাদেশ.icom.museum', | ||
'беларусь.icom.museum', | ||
'belgië.icom.museum', | ||
'българия.icom.museum', | ||
'تشادر.icom.museum', | ||
'中国.icom.museum', | ||
'القمر.icom.museum', | ||
'κυπρος.icom.museum', | ||
'českárepublika.icom.museum', | ||
'مصر.icom.museum', | ||
'ελλάδα.icom.museum', | ||
'magyarország.icom.museum', | ||
'ísland.icom.museum', | ||
'भारत.icom.museum', | ||
'ايران.icom.museum', | ||
'éire.icom.museum', | ||
'איקו״ם.ישראל.museum', | ||
'日本.icom.museum', | ||
'الأردن.icom.museum' | ||
] | ||
}); | ||
|
||
function usingPunycode(val) { | ||
punycode.toUnicode(punycode.toASCII(val)); | ||
} | ||
|
||
function usingICU(val) { | ||
icu.toUnicode(icu.toASCII(val)); | ||
} | ||
|
||
function runPunycode(n, val) { | ||
common.v8ForceOptimization(usingPunycode, val); | ||
var i = 0; | ||
bench.start(); | ||
for (; i < n; i++) | ||
usingPunycode(val); | ||
bench.end(n); | ||
} | ||
|
||
function runICU(n, val) { | ||
common.v8ForceOptimization(usingICU, val); | ||
var i = 0; | ||
bench.start(); | ||
for (; i < n; i++) | ||
usingICU(val); | ||
bench.end(n); | ||
} | ||
|
||
function main(conf) { | ||
const n = +conf.n; | ||
const val = conf.val; | ||
switch (conf.method) { | ||
case 'punycode': | ||
runPunycode(n, val); | ||
break; | ||
case 'icu': | ||
runICU(n, val); | ||
break; | ||
default: | ||
throw new Error('Unexpected method'); | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const icu = getPunycode(); | ||
const assert = require('assert'); | ||
|
||
function getPunycode() { | ||
try { | ||
return process.binding('icu'); | ||
} catch (err) { | ||
return undefined; | ||
} | ||
} | ||
|
||
if (!icu) { | ||
common.skip('icu punycode tests because ICU is not present.'); | ||
return; | ||
} | ||
|
||
// Credit for list: http://www.i18nguy.com/markup/idna-examples.html | ||
const tests = [ | ||
'افغانستا.icom.museum', | ||
'الجزائر.icom.museum', | ||
'österreich.icom.museum', | ||
'বাংলাদেশ.icom.museum', | ||
'беларусь.icom.museum', | ||
'belgië.icom.museum', | ||
'българия.icom.museum', | ||
'تشادر.icom.museum', | ||
'中国.icom.museum', | ||
'القمر.icom.museum', | ||
'κυπρος.icom.museum', | ||
'českárepublika.icom.museum', | ||
'مصر.icom.museum', | ||
'ελλάδα.icom.museum', | ||
'magyarország.icom.museum', | ||
'ísland.icom.museum', | ||
'भारत.icom.museum', | ||
'ايران.icom.museum', | ||
'éire.icom.museum', | ||
'איקו״ם.ישראל.museum', | ||
'日本.icom.museum', | ||
'الأردن.icom.museum', | ||
'қазақстан.icom.museum', | ||
'한국.icom.museum', | ||
'кыргызстан.icom.museum', | ||
'ລາວ.icom.museum', | ||
'لبنان.icom.museum', | ||
'македонија.icom.museum', | ||
'موريتانيا.icom.museum', | ||
'méxico.icom.museum', | ||
'монголулс.icom.museum', | ||
'المغرب.icom.museum', | ||
'नेपाल.icom.museum', | ||
'عمان.icom.museum', | ||
'قطر.icom.museum', | ||
'românia.icom.museum', | ||
'россия.иком.museum', | ||
'србијаицрнагора.иком.museum', | ||
'இலங்கை.icom.museum', | ||
'españa.icom.museum', | ||
'ไทย.icom.museum', | ||
'تونس.icom.museum', | ||
'türkiye.icom.museum', | ||
'украина.icom.museum', | ||
'việtnam.icom.museum' | ||
]; | ||
|
||
// Testing the roundtrip | ||
tests.forEach((i) => { | ||
assert.strictEqual(i, icu.toUnicode(icu.toASCII(i))); | ||
}); |
This file contains 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
This file contains 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