String Format Vs Concat

JavaScript performance comparison

Test case created by Josh Dean

Info

Testing the performance differences between having a custom string formatting function versus doing plain string concatenation

Preparation code

<script>
  String.prototype.format = function() {
   var frmt = "";
   for (arg in arguments) {
    frmt = (frmt == "" ? this : frmt).replace(RegExp("[{]" + arg + "[}]", "gi"), arguments[arg]);
   }
   return frmt;
  };
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
Format
var str = "a[rel*={0}]",
    str2 = "title",
    count = 100;

while (count--) {
 str.format(str2);
}
pending…
Concatenation
var str = "",
    str2 = "title",
    count = 100;

while (count--) {
 str = "a[rel*=" + str2 + "]";
}
pending…

You can edit these tests or add even more tests to this page by appending /edit to the URL.

Compare results of other browsers

0 comments

Add a comment