A high performance string format function for Javascript

Source: Internet
Author: User
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 parse 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 shocould 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', '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 $ shocould 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 shocould 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 );
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.