用户操作
[即时聊天] [发私信] [加为好友]
贺师俊ID:hax
115181次访问,排名752好友1人,关注者10
hax的文章
原创 124 篇
翻译 2 篇
转载 12 篇
评论 128 篇
hax的公告

我回来了……

最近评论
sap99:www.sap99.com/,SAP99资料多多

SAP免费资料下载
http://www.sap99.com

有很多的学习资料,推荐一下,
hax:其次,也并不是一定要切换编码。只要你的系统是遵循既有规范的,则可以无缝的整合。
hax:从gb2312切换到utf-8其实并不难,有个几天就可以了。
allskystar:以我现在的公司为例,主要的问题在于,以前已经有几个很大的系统用了gb2312了,现在新的系统用的utf-8,要兼容,如果我对头儿说要花一年时间把以前的几个系统都改成utf-8的,估计会被他从10楼直接扔下去...
hax:哦,qomo终于不用eval啦?
文章分类
收藏
    相册
    blog图片
    More
    sfo正牌blog(RSS)
    个人网站
    我在JavaEye的技术部落格
    我墙外的Blogger
    存档
    软件项目交易
    订阅我的博客
    XML聚合  FeedSky
    订阅到鲜果
    订阅到Google
    订阅到抓虾
    订阅到BlogLines
    订阅到Yahoo
    订阅到GouGou
    订阅到飞鸽
    订阅到Rojo
    订阅到newsgator
    订阅到netvibes

    原创 A high performance string format function for JavaScript收藏

    新一篇: 牛人就是牛人 | 旧一篇: JSIntegration

    2006-11-30 Updated: I also write another high performance string format function which compiles the pattern into function and cache it for reuse.


    Last month, I wrote a logging tool for js, and to avoid performance depression, I need a string formatter function.
    I found that though many js toolkit or framework provide string format function, such as Atlas, but they r not quite fast. Most r using String.replace(regex, func) to replace the placeholder(such as '{n}' or '$n'), but this function tend to slow, because every match will call the func, and js function calling is very expensive.
    On the contrary, native functions r very fast, so to improve performance, we should utilize the native functions as possible as we can. A wonderful example is StringBuilder which use Array.join to concat the string.

    So I create my String.format(), it's very fast.

    Usage:

    var name = 'world';
    var result = 'Hello $1!'.format(name);
    // result = "Hello world!"

    var letters = String.format('$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', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z');
    // letters = "abcdefghijklmnopqrstuvwxyz"

    The later one almost same fast as the former one, no other implementation can have the same performance as I know.

    Note:
    1. It's depend on String.replace(regex, string), so u can use at most 99 placeholder($1 to $99), but if the script engine is too old, it maybe only support nine ($1 to $9) or not work at all (eg. JScript before 5.5?).
    2. literal $ should be escape into $$ (two $).
    3. $` and $' will be reomoved, and $& will replaced into some strange things :)
    4. There is a magic character which you can't use anyway, currently I choose 0x1f (which means data separator in ascii and unicode).

    2006-11-30 Updated:
    '$1 1'.format('a') result in 'a 1', if you want to strip the space, u can't write '$11'.format(...) because it will try to match the 11nd parameter, u should write '$011'.format(...) instead.

    Source code:

    // Copyright (c) HE Shi-Jun <hax.sfo at gmail dot com>, 2006
    // Below codes can be used under GPL (v2 or later) or LGPL (v2.1 or later) license

        if (!String._FORMAT_SEPARATOR) {
            String._FORMAT_SEPARATOR 
    = String.fromCharCode(0x1f);
            String._FORMAT_ARGS_PATTERN 
    = new RegExp('^[^' + String._FORMAT_SEPARATOR + ']*'
                
    + new Array(100).join('(?:.([^' + String._FORMAT_SEPARATOR + ']*))?'));
        }

        
    if (!String.format)
        String.format 
    = function (s) {
            
    return Array.prototype.join.call(arguments, String._FORMAT_SEPARATOR).
                replace(String._FORMAT_ARGS_PATTERN, s);
        }

        
    if (!''.format)
        String.prototype.format 
    = function () {
            
    return (String._FORMAT_SEPARATOR +
                Array.prototype.join.call(arguments, String._FORMAT_SEPARATOR)).
                replace(String._FORMAT_ARGS_PATTERN, 
    this);
        }

        

    发表于 @ 2006年11月27日 13:57:00|评论(loading...)|编辑

    新一篇: 牛人就是牛人 | 旧一篇: JSIntegration

    评论

    #hax 发表于2006-11-27 14:37:00  IP: 61.172.241.*
    为什么这个实现的性能胜过其他?其实很简单,观察其实现代码,每次format调用所作的只是一次Array.join然后一次 String.replace(regex, string)的操作,两者都是js中的native方法,而不会有任何自定义函数调用。相反的,在一般的实现中,使用的是String.replace(regex, func),在pattern包含n个占位符(包括重复的)时,func就被调用n次,加上有些实现中很可能要执行多次replace(占位符编号到几,就要几次),这样性能焉能不迅速降低?
    #客户端的东西 发表于2006-11-28 09:17:00  IP: 221.237.164.*
    有这个必要....
    #chine 发表于2006-11-28 14:01:00  IP: 61.186.177.*
    good trick, but it is a pseudo format function :)
    #涓€鍒囩殕鏈夊彲鑳絶 发表于2006-11-29 09:26:00  IP: 221.10.47.*
    function oStringBuffer(){

    this._strings_ = new Array;

    if( typeof oStringBuffer._initialized == "undefined"){

    oStringBuffer.prototype.append=function(str){
    this._strings_.push(str);
    };

    oStringBuffer.prototype.toString=function(){
    return this._strings_.join("");
    };

    oStringBuffer._initialized=true;
    }
    }
    发表评论  


    当前用户设置只有注册用户才能发表评论。如果你没有登录,请点击登录
    Csdn Blog version 3.1a
    Copyright © hax