Insert char at given position in string

JavaScript performance comparison

Revision 2 of this test case created by JM


It is cheaper to insert chart at given position using substr and + or it is better to split the string, splice it and then join?

Preparation code

Benchmark.prototype.setup = function() {
    var content = "This is my string, which is that long";

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
using substr and +
content.substring(0, 18) + "$" + content.substr(18);
using split, splice, join
var arr = content.split("");

arr.splice(18, 0, "$");


Compare results of other browsers


You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:


Comment form temporarily disabled.

Add a comment