replace vs string concat

JavaScript performance comparison

Test case created by Ward

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    charLimitErrorMsg = 'Oops!  The personal message can only be %l characters long. Please shorten your message.';
   
    msgStart = 'Oops!  The personal message can only be ';
   
    msgEnd = 'characters long. Please shorten your message.';
   
    rx = /%l/;
   
    repStr = '57';
   
    repInt = 57;
};
</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
Replace with int
charLimitErrorMsg.replace(rx, repInt);
pending…
Replace with str
charLimitErrorMsg.replace(rx, repStr);
pending…
String concat w/ int
msgStart + repInt + msgEnd;
pending…
String concat with str
msgStart + repStr + msgEnd;
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