Replace regex vs. string (v4)

Revision 4 of this benchmark created by Tim Tucker on


Setup

var re = RegExp("test[123]", "g");
    
    var indexOfStringReplace = function(val) {
        if (val.indexOf("test1") != -1)
        {
            val.replace("test1", "");
        }
        else if (val.indexOf("test2") != -1)
        {
            val.replace("test2", "");
        }
        if (val.indexOf("test3") != -1)
        {
            val.replace("test3", "");
        }
    };
    
    var regexReplace = function(val) {
        if (val.indexOf("test", 0) != -1)
        {
            val.replace(re, "");
        }
    };
    
    var greedyRegexReplace = function(val) {
        if (val.indexOf("test", 0) != -1)
        {
            val.replace(re2, "");
        }
    };
    
    var chainedReplace = function(val) {
        if (val.indexOf("test", 0) != -1)
        {
            val.replace("test1", "").replace("test2", "").replace("test3", "");
        }
    };
    
    var splitJoin = function(val) {
        if (val.indexOf("test", 0) != -1)
        {
            val = val.split("test1").join("").split("test2").join("").split("test3").join("");
        }
    };
    
    var splitJoinRegex = function(val) {
        if (val.indexOf("test", 0) != -1)
        {
            val = val.split(re).join("");
        }
    };

Test runner

Ready to run.

Testing in
TestOps/sec
indexOf String replace
indexOfStringReplace("myClass otherClass test1");
indexOfStringReplace("myClass otherClass test3");
indexOfStringReplace("myClass otherClass");
ready
regex replace
regexReplace("myClass otherClass test1");
regexReplace("myClass otherClass test3");
regexReplace("myClass otherClass");
ready
chained replace
chainedReplace("myClass otherClass test1");
chainedReplace("myClass otherClass test3");
chainedReplace("myClass otherClass");
ready
splitJoin
splitJoin("myClass otherClass test1");
splitJoin("myClass otherClass test3");
splitJoin("myClass otherClass");
ready
splitJoin regex
splitJoinRegex("myClass otherClass test1");
splitJoinRegex("myClass otherClass test3");
splitJoinRegex("myClass otherClass");
ready

Revisions

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

  • Revision 4: published by Tim Tucker on