渡辺コンバーターをもっと高速に
スポンサードリンク
いきさんところ
渡辺コンバーター - k_ikiの日記 [d.hatena.ne.jp]
で出したコード
//タグ付きテキストの内容を変換する関数
function text_replace (text) {
var _replace = function (a,b) {
switch (b) {
case "02": return "<clig:0><cotfcalt:0>邉<clig:><cotfcalt:>";
case "03": return "<clig:0><cotfcalt:0><calgf:JIS78Form>邉<clig:><cotfcalt:><calgf:>";
case "04": return "<clig:0><cotfcalt:0><cotfl:aalt\,4>邉<clig:><cotfcalt:><cotfl:>";
case "05": return "<clig:0><cotfcalt:0><cotfl:aalt\,5>邉<clig:><cotfcalt:><cotfl:>";
case "06": return "<clig:0><cotfcalt:0><cotfl:aalt\,6>邉<clig:><cotfcalt:><cotfl:>";
case "07": return "<clig:0><cotfcalt:0><cotfl:aalt\,7>邉<clig:><cotfcalt:><cotfl:>";
case "08": return "<clig:0><cotfcalt:0><cotfl:aalt\,8>邉<clig:><cotfcalt:><cotfl:>";
case "09": return "<clig:0><cotfcalt:0><cotfl:aalt\,9>邉<clig:><cotfcalt:><cotfl:>";
case "10": return "<clig:0><cotfcalt:0><cotfl:aalt\,10>邉<clig:><cotfcalt:><cotfl:>";
case "11": return "<clig:0><cotfcalt:0><cotfl:aalt\,11>邉<clig:><cotfcalt:><cotfl:>";
}
return a;
};
return text.replace(/辺_(\d\d)/g, _replace);
}
は、
//タグ付きテキストの内容を変換する関数
var text_replace = (function(){
var _replace = function (a,b) {
switch (b) {
case "02": return "<clig:0><cotfcalt:0>邉<clig:><cotfcalt:>";
case "03": return "<clig:0><cotfcalt:0><calgf:JIS78Form>邉<clig:><cotfcalt:><calgf:>";
case "04": return "<clig:0><cotfcalt:0><cotfl:aalt\,4>邉<clig:><cotfcalt:><cotfl:>";
case "05": return "<clig:0><cotfcalt:0><cotfl:aalt\,5>邉<clig:><cotfcalt:><cotfl:>";
case "06": return "<clig:0><cotfcalt:0><cotfl:aalt\,6>邉<clig:><cotfcalt:><cotfl:>";
case "07": return "<clig:0><cotfcalt:0><cotfl:aalt\,7>邉<clig:><cotfcalt:><cotfl:>";
case "08": return "<clig:0><cotfcalt:0><cotfl:aalt\,8>邉<clig:><cotfcalt:><cotfl:>";
case "09": return "<clig:0><cotfcalt:0><cotfl:aalt\,9>邉<clig:><cotfcalt:><cotfl:>";
case "10": return "<clig:0><cotfcalt:0><cotfl:aalt\,10>邉<clig:><cotfcalt:><cotfl:>";
case "11": return "<clig:0><cotfcalt:0><cotfl:aalt\,11>邉<clig:><cotfcalt:><cotfl:>";
}
return a;
};
return function(text) {return text.replace(/辺_(\d\d)/g, _replace);};
})();
と書き換えて、こっちの方が高速だけれども、ここまで来ると記述順により動かなかったりするし可読性悪いと思うのでやめたほうがいいかも。
なんで高速になるかっていうのは、
Life is beautiful: Javascript、クロージャを使ったプライベート関数の隠蔽について [satoshi.blogs.com]
の例がそのまま当てはまる例だからだと思います。実際1000回ループでベンチとったら後者の方が速かったよ。
スポンサードリンク
トラックバック(0)
トラックバックURL: http://blog.dtpwiki.jp/MTOS/mt-tb.cgi/2655





![: Amazon.co.jp: プラスティック・メモリーズ 1【完全生産限定版】(イベントチケット優先販売申込券付) [Blu-ray]](/lists/_9/B00VWX66E8.jpg)
![: Amazon.co.jp: プラスティック・メモリーズ 2【完全生産限定版】[Blu-ray]](/lists/_9/B00VWX66K2.jpg)
![: Amazon.co.jp: プラスティック・メモリーズ 3【完全生産限定版】[Blu-ray]](/lists/_9/B00VWX6MV0.jpg)
![: Amazon.co.jp: プラスティック・メモリーズ 4【完全生産限定版】[Blu-ray]](/lists/_9/B00VWX66IO.jpg)
![: Amazon.co.jp: プラスティック・メモリーズ 5【完全生産限定版】[Blu-ray]](/lists/_9/B00VWX6Y0E.jpg)
![: Amazon.co.jp: プラスティック・メモリーズ 6【完全生産限定版】[Blu-ray]](/lists/_9/B00VWX69D6.jpg)


コメントする