Replace All Character Instances in String

Benchmark created by Nathan on


Preparation HTML

<script>
var string = 'btn-success',
    rgxAllDots = /\./g;

(function() {
"use strict";

String.prototype.replaceAllChar = function(character, replacement) {
    var indices = [],
        i = 0,
        str = '';
    for (; i < this.length; i++) {
        if (this[i] === character) {
            indices.push(i);
        }
    }
    for (i = 0; i < indices.length;) {
        str += replacement + this.slice(indices[i] + 1, indices[++i]);
    }
    return str || this;
};

})();
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
Native
string.replace(rgxAllDots, ' ');
ready
replaceAllChar
string.replaceAllChar('.', ' ');
ready

Revisions

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