replace vs string concat

JavaScript performance comparison

Test case created by Ward

Preparation code

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;

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);
Replace with str
charLimitErrorMsg.replace(rx, repStr);
String concat w/ int
msgStart + repInt + msgEnd;
String concat with str
msgStart + repStr + msgEnd;

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

Compare results of other browsers


Comment form temporarily disabled.

Add a comment