Build a regexp table
JavaScript performance comparison
Info
The fastest way to build a replacing function based on regexps.
See replacer in https://github.com/NaturalNode/natural/blob/master/lib/natural/util/utils.js
This checks only the building of replacing functions, not its actual use.
Preparation code
<script>
var table = {"㋀":"1月","㋁":"2月","㋂":"3月","㋃":"4月","㋄":"5月","㋅":"6月","㋆":"7月","㋇":"8月","㋈":"9月","㋉":"10月","㋊":"11月","㋋":"12月","㏠":"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日","㍘":"0点","㍙":"1点","㍚":"2点","㍛":"3点","㍜":"4点","㍝":"5点","㍞":"6点","㍟":"7点","㍠":"8点","㍡":"9点","㍢":"10点","㍣":"11点","㍤":"12点","㍥":"13点","㍦":"14点","㍧":"15点","㍨":"16点","㍩":"17点","㍪":"18点","㍫":"19点","㍬":"20点","㍭":"21点","㍮":"22点","㍯":"23点","㍰":"24点","㍻":"平成","㍼":"昭和","㍽":"大正","㍾":"明治","㍿":"株式会社","㌀":"アパート","㌁":"アルファ","㌂":"アンペア","㌃":"アール","㌄":"イニング","㌅":"インチ","㌆":"ウオン","㌇":"エスクード","㌈":"エーカー","㌉":"オンス","㌊":"オーム","㌋":"カイリ","㌌":"カラット","㌍":"カロリー","㌎":"ガロン","㌏":"ガンマ","㌐":"ギガ","㌑":"ギニー","㌒":"キュリー","㌓":"ギルダー","㌔":"キロ","㌕":"キログラム","㌖":"キロメートル","㌗":"キロワット","㌘":"グラム","㌙":"グラムトン","㌚":"クルゼイロ","㌛":"クローネ","㌜":"ケース","㌝":"コルナ","㌞":"コーポ","㌟":"サイクル","㌠":"サンチーム","㌡":"シリング","㌢":"センチ","㌣":"セント","㌤":"ダース","㌥":"デシ","㌦":"ドル","㌧":"トン","㌨":"ナノ","㌩":"ノット","㌪":"ハイツ","㌫":"パーセント","㌬":"パーツ","㌭":"バーレル","㌮":"ピアストル","㌯":"ピクル","㌰":"ピコ","㌱":"ビル","㌲":"ファラッド","㌳":"フィート","㌴":"ブッシェル","㌵":"フラン","㌶":"ヘクタール","㌷":"ペソ","㌸":"ペニヒ","㌹":"ヘルツ","㌺":"ペンス","㌻":"ページ","㌼":"ベータ","㌽":"ポイント","㌾":"ボルト","㌿":"ホン","㍀":"ポンド","㍁":"ホール","㍂":"ホーン","㍃":"マイクロ","㍄":"マイル","㍅":"マッハ","㍆":"マルク","㍇":"マンション","㍈":"ミクロン","㍉":"ミリ","㍊":"ミリバール","㍋":"メガ","㍌":"メガトン","㍍":"メートル","㍎":"ヤード","㍏":"ヤール","㍐":"ユアン","㍑":"リットル","㍒":"リラ","㍓":"ルピー","㍔":"ルーブル","㍕":"レム","㍖":"レントゲン","㍗":"ワット"};
function replacer1(translationTable) {
/**
* An array of translationTable keys.
* @type {Array.<string>}
*/
var pattern = [];
/**
* The regular expression doing the replacement job.
* @type {RegExp}
*/
var regExp;
/**
* Used to iterate over translationTable.
* @type {string}
*/
var key;
for (key in translationTable) {
// Escaping regexp special chars.
key = ('' + key).replace(/([-()\[\]{}+?*.$\^|,:#<!\\])/g, '\\$1').
replace(/\x08/g, '\\x08');
pattern.push(key);
}
regExp = new RegExp(pattern.join('|'), 'g');
/**
* @param {string} str Input string.
* @return {string} The string replaced.
*/
return function(str) {
return str.replace(regExp, function(str) {
return translationTable[str];
});
};
}
function replacer2(translationTable) {
/**
* The regular expression doing the replacement job.
* @type {RegExp}
*/
var regExp;
/**
* An array containing the keys of the replacement table.
* @type {Array.<string>}
*/
var keys = Object.keys(translationTable);
keys = keys.map(function(key) {
return ('' + key).replace(/([-()\[\]{}+?*.$\^|,:#<!\\])/g, '\\$1').
replace(/\x08/g, '\\x08');
});
regExp = new RegExp(keys.join('|'), 'g');
/**
* @param {string} str Input string.
* @return {string} The string replaced.
*/
return function(str) {
return str.replace(regExp, function(str) {
return translationTable[str];
});
};
}
function replacer3(translationTable) {
/**
* The regular expression doing the replacement job.
* @type {RegExp}
*/
var regExp;
/**
* An array containing the keys of the replacement table.
* @type {Array.<string>}
*/
var keys = Object.keys(translationTable);
for (var i = 0, len = keys.length; i < len; i++) {
keys[i] = ('' + keys[i]).replace(/([-()\[\]{}+?*.$\^|,:#<!\\])/g, '\\$1').
replace(/\x08/g, '\\x08');
}
regExp = new RegExp(keys.join('|'), 'g');
/**
* @param {string} str Input string.
* @return {string} The string replaced.
*/
return function(str) {
return str.replace(regExp, function(str) {
return translationTable[str];
});
};
}
</script>
Test runner
Warning! For accurate results, please disable Firebug before running the tests. (Why?)
Java applet disabled.
| Test | Ops/sec | |
|---|---|---|
For in loop based |
|
pending… |
Object.keys/Array.map based |
|
pending… |
Object.keys/for loop based |
|
pending… |
Compare results of other browsers
Revisions
You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:
- Revision 1: published
- Revision 2: published
0 comments