Insert char at given position in string

JavaScript performance comparison

Revision 2 of this test case created by JM

Info

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

 
<script>
Benchmark.prototype.setup = function() {
    var content = "This is my string, which is that long";
};
</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
using substr and +
content.substring(0, 18) + "$" + content.substr(18);
pending…
using split, splice, join
var arr = content.split("");

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

arr.join("");
pending…

Compare results of other browsers

Revisions

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:

0 comments

Add a comment