A high performance string format function for JavaScript

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);
    }

    
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值