format vs replace vs regex (v5)

Revision 5 of this benchmark created by Eyal Shilony on


Setup

(function () {
        String.prototype.format1 = function () {
            // todo: Throw exception when we don't support the type of the argument
            // todo: Throw exception when this is not a string
            var args = [];
            for (var _i = 0; _i < (arguments.length - 0); _i++) {
                args[_i] = arguments[_i + 0];
            }
            var input = this;
            var inputLength = input.length;
            var output = input;
            var token = "";
    
            for (var i = 0; i < inputLength; i++) {
                var ch = input[i];
                var tokenLength = token.length;
    
                switch (ch) {
                    case "{":
                        token = ch;
                        break;
                    case "}":
                        var value = "";
                        if (tokenLength > 0) {
                            token += ch;
                            var index = token.match(/\{(\d+)\}/);
                            if (index != null && index.length === 2) {
                                // todo: Throw exception if the index is greater than the length of the argument list
                                value = args[index[1]];
                            }
                        }
                        output = output.replace(token, value);
                        token = "";
                        break;
                    default: {
                        if (tokenLength > 0) {
                            token += ch;
                        }
                    }
                }
            }
    
            return output;
        };
    })();
    
    (function () {
        var isDigit = /\d/;
        var toNumber = Number;
        var pow = Math.pow;
    
        String.prototype.format2 = function () {
            // todo: Throw an exception when we don't support the type of the argument
            var args = [];
            for (var _i = 0; _i < (arguments.length - 0); _i++) {
                args[_i] = arguments[_i + 0];
            }
            var input = this;
            var inputLength = input.length;
    
            var output = "";
            var token = "";
    
            for (var i = 0; i < inputLength; i++) {
                var ch = input[i];
                var tokenLength = token.length;
    
                switch (ch) {
                    case "{":
                        if (tokenLength > 0) {
                            output += token;
                        }
                        token = ch;
                        break;
                    case "}":
                        if (tokenLength > 0) {
                            var index = 0;
                            var length = tokenLength - 1;
    
                            for (var n = 0; n < length; n++) {
                                index += pow(10, n) * toNumber(token[length - n]);
                            }
    
                            // todo: Throw exception if the index is greater than the length of the argument list
                            output += args[index];
    
                            token = "";
                        } else {
                            output += ch;
                        }
                        break;
                    default: {
                        if (tokenLength > 0) {
                            if (isDigit.test(ch)) {
                                token += ch;
                            } else {
                                output += token + ch;
                                token = "";
                            }
                        } else {
                            output += ch;
                        }
                    }
                }
            }
    
            return output;
        };
    })();
    
    (function () {
        String.prototype.format3 = function (args) {
                var str = this;
                return str.replace(String.prototype.format3.regex, function(item) {
                        var intVal = parseInt(item.substring(1, item.length - 1));
                        var replace;
                        if (intVal >= 0) {
                                replace = args[intVal];
                        } else if (intVal === -1) {
                                replace = "{";
                        } else if (intVal === -2) {
                                replace = "}";
                        } else {
                                replace = "";
                        }
                        return replace;
                });
        };
        String.prototype.format3.regex = new RegExp("{-?[0-9]+}", "g");
    })();
    
    (function () {
        String.prototype.format4 = function() {
                var formatted = this;
                for (var i = 0; i < arguments.length; i++) {
                        var regexp = new RegExp('\\{'+i+'\\}', 'gi');
                        formatted = formatted.replace(regexp, arguments[i]);
                }
                return formatted;
        };
    })();
    
    (function () {
         String.prototype.format5 = function() {
            var str = this.toString();
            if (!arguments.length)
                return str;
            var args = typeof arguments[0],
                args = (("string" == args || "number" == args) ? arguments : arguments[0]);
            for (arg in args)
                str = str.replace(RegExp("\\{" + arg + "\\}", "gi"), args[arg]);
            return str;
        };
    })();
    
    (function () {
        var isDigit = /\d/;
        var toNumber = Number;
        var pow = Math.pow;
    
        String.prototype.format6 = function () {
            // todo: Throw exception when we don't support the type of the argument
            // todo: Throw exception when this is not a string
            var args = [];
            for (var _i = 0; _i < (arguments.length - 0); _i++) {
                args[_i] = arguments[_i + 0];
            }
            var input = this;
            var inputLength = input.length;
            var output = input;
            var token = "";
    
            for (var i = 0; i < inputLength; i++) {
                var ch = input[i];
                var tokenLength = token.length;
    
                switch (ch) {
                    case "{":
                        token = ch;
                        break;
                    case "}":
                        var value = "";
                        if (tokenLength > 0) {
                            var index = 0;
                            var length = tokenLength - 1;
    
                            for (var n = 0; n < length; n++) {
                                index += pow(10, n) * toNumber(token[length - n]);
                            }
    
                            // todo: Throw exception if the index is greater than the length of the argument list
                            value = args[index];
    
                            token += ch;
                        }
                        output = output.replace(token, value);
                        token = "";
                        break;
                    default: {
                        if (tokenLength > 0) {
                            if (isDigit.test(ch)) {
                                token += ch;
                            } else {
                                token = "";
                            }
                        }
                    }
                }
            }
    
            return output;
        };
    })();
    
    (function () {
        String.prototype.format7 = function() {
                var formatted = this;
                for (var i = 0; i < arguments.length; i++) {
                        formatted = formatted.replace("{" + i + "}", arguments[i]);
                }
                return formatted;
        };
    })();
    
    (function () {
        String.prototype.format8 = function (pattern) {
            // todo: Throw exception when we don't support the type of the argument
            // todo: Throw exception when this is not a string
            var args = [];
            for (var _i = 0; _i < (arguments.length - 1); _i++) {
                args[_i] = arguments[_i + 1];
            }
            var input = pattern;
            var inputLength = input.length;
            var output = input;
            var token = "";
    
            for (var i = 0; i < inputLength; i++) {
                var ch = input[i];
                var tokenLength = token.length;
    
                switch (ch) {
                    case "{":
                        token = ch;
                        break;
                    case "}":
                        var value = "";
                        if (tokenLength > 0) {
                            token += ch;
                            var index = token.match(/\{(\d+)\}/);
                            if (index != null && index.length === 2) {
                                // todo: Throw exception if the index is greater than the length of the argument list
                                value = args[index[1]];
                            }
                        }
                        output = output.replace(token, value);
                        token = "";
                        break;
                    default: {
                        if (tokenLength > 0) {
                            token += ch;
                        }
                    }
                }
            }
    
            return output;
        };
    })();
    
    (function () {
        String.prototype.format9 = function () {
            // todo: Throw exception when we don't support the type of the argument
            // todo: Throw exception when this is not a string
            var args = [];
            for (var _i = 0; _i < (arguments.length - 0); _i++) {
                args[_i] = arguments[_i + 0];
            }
            var input = this;
            var inputLength = input.length;
            var output = input;
            var token = "";
    
            for (var i = 0; i < inputLength; i++) {
                var ch = input.charAt(i);
                var tokenLength = token.length;
    
                switch (ch) {
                    case "{":
                        token = ch;
                        break;
                    case "}":
                        var value = "";
                        if (tokenLength > 0) {
                            token += ch;
                            var index = token.match(/\{(\d+)\}/);
                            if (index != null && index.length === 2) {
                                // todo: Throw exception if the index is greater than the length of the argument list
                                value = args[index[1]];
                            }
                        }
                        output = output.replace(token, value);
                        token = "";
                        break;
                    default: {
                        if (tokenLength > 0) {
                            token = token.concat(ch);
                        }
                    }
                }
            }
    
            return output;
        };
    })();
    
    (function () {
        String.prototype.format10 = function () {
            // todo: Throw exception when we don't support the type of the argument
            // todo: Throw exception when this is not a string
            var args = [];
            for (var _i = 0; _i < (arguments.length - 0); _i++) {
                args[_i] = arguments[_i + 0];
            }
            var input = this;
            var inputLength = input.length;
            var output = input;
            var token = "";
    
            for (var i = 0; i < inputLength; i++) {
                var ch = input.charAt(i);
                var tokenLength = token.length;
    
                switch (ch) {
                    case "{":
                        token = ch;
                        break;
                    case "}":
                        var value = "";
                        if (tokenLength > 0) {
                            token += ch;
                            var index = token.match(/\{(\d+)\}/);
                            if (index != null && index.length === 2) {
                                // todo: Throw exception if the index is greater than the length of the argument list
                                value = args[index[1]];
                            }
                        }
                        output = output.replace(token, value);
                        token = "";
                        break;
                    default: {
                        if (tokenLength > 0) {
                            token += ch;
                        }
                    }
                }
            }
    
            return output;
        };
    })();
    
    (function () {
        String.prototype.format11 = function () {
            // todo: Throw exception when we don't support the type of the argument
            // todo: Throw exception when this is not a string
            var args = [];
            for (var _i = 0; _i < (arguments.length - 0); _i++) {
                args[_i] = arguments[_i + 0];
            }
            var input = this;
            var inputLength = input.length;
    
            var output = this;
    
            var token = "";
            var tokenLength;
    
            var ch;
    
            for (var i = 0; i < inputLength; i++) {
                ch = input.charAt(i);
                tokenLength = token.length;
    
                switch (ch) {
                    case "{":
                        token = ch;
                        break;
                    case "}":
                        var value = "";
                        if (tokenLength > 0) {
                            token = token.concat(ch);
                            var index = token.match(/\{(\d+)\}/);
                            if (index != null && index.length === 2) {
                                // todo: Throw exception if the index is greater than the length of the argument list
                                value = args[index[1]];
                            }
                        }
                        output = output.replace(token, value);
                        token = "";
                        break;
                    default: {
                        if (tokenLength > 0) {
                            token = token.concat(ch);
                        }
                    }
                }
            }
    
            return output;
        };
    })();
    
    (function () {
        String.prototype.format12 = function () {
            // todo: Throw exception when we don't support the type of the argument
            var args = [];
            for (var r = 0; r < arguments.length; r++) {
                args[r] = arguments[r];
            }
    
            // todo: Throw exception when this is not a string
            var input = this;
            var inputLength = input.length;
    
            var output = this;
    
            var token = "";
            var tokenLength;
    
            var index = "";
            var value;
    
            var ch;
    
            for (var i = 0; i < inputLength; i++) {
                ch = input.charAt(i);
                tokenLength = token.length;
    
                switch (ch) {
                    case "{":
                        token = ch;
                        break;
                    case "}":
                        value = index = "";
                        if (tokenLength > 0) {
                            token = token.concat(ch);
    
                            for (var j = 1; j <= token.search(/\d\b/); j++) {
                                index = index.concat(token[j]);
                            }
    
                            // todo: Throw exception if the index is greater than the length of the argument list
                            if (index != "") {
                                value = args[index];
                            }
                        }
                        output = output.replace(token, value);
                        token = "";
                        break;
                    default: {
                        if (tokenLength > 0) {
                            token = token.concat(ch);
                        }
                    }
                }
            }
    
            return output;
        };
    })();
    
    (function () {
        String.prototype.format13 = function () {
            // todo: Throw exception when we don't support the type of the argument
            var args = [];
            for (var r = 0; r < arguments.length; r++) {
                args[r] = arguments[r];
            }
    
            // todo: Throw exception when this is not a string
            var input = this;
            var inputLength = input.length;
    
            var output = this;
    
            var token = "";
            var tokenLength;
    
            var index;
            var value;
    
            var ch;
    
            for (var i = 0; i < inputLength; i++) {
                ch = input[i];
                tokenLength = token.length;
    
                switch (ch) {
                    case "{":
                        token = ch;
                        break;
                    case "}":
                        value = index = "";
                        if (tokenLength > 0) {
                            token += ch;
    
                            for (var j = 1; j <= token.search(/\d\b/); j++) {
                                index += token[j];
                            }
    
                            // todo: Throw exception if the index is greater than the length of the argument list
                            if (index != "") {
                                value = args[index];
                            }
                        }
                        output = output.replace(token, value);
                        token = "";
                        break;
                    default: {
                        if (tokenLength > 0) {
                            token += ch;
                        }
                    }
                }
            }
    
            return output;
        };
    })();
    
    (function () {
        var regex = /\{(\d+)(?:\:(.))?\}/gi;
    
        String.prototype.formatX = function () {
            // todo: Throw exception when we don't support the type of the argument
            var args = [];
            for (var r = 0; r < arguments.length; r++) {
                args[r] = arguments[r];
            }
    
            // todo: Throw exception when this is not a string
            var input = this;
            return input.replace(regex, function (match, index, format) {
                var value = "";
    
                // Convert the string to number;
                index = index * 1;
    
                // todo: Throw exception if the index is greater than the length of the argument list
                if (index >= 0) {
                    value = args[index];
                }
    
                return value;
            });
        };
    })();
    
    (function () {
        var utils = {};
    
        var regex = /\{(\d+)(?:\:(.))?\}/gi;
    
        utils.format = function (pattern) {
            // todo: Throw exception when we don't support the type of the argument
            var args = [];
            for (var r = 0; r < (arguments.length - 1); r++) {
                args[r] = arguments[r + 1];
            }
    
            // todo: Throw exception when this is not a string
            return pattern.replace(regex, function (match, index, format) {
                var value = "";
    
                // Convert the string to number;
                index = index * 1;
    
                // todo: Throw exception if the index is greater than the length of the argument list
                if (index >= 0) {
                    value = args[index];
                }
    
                return value;
            });
        };
    
        window.utils = window.utils || utils;
    })();
    
    (function () {
        var utils = {};
    
        var regex = /\{(\d+)(?:\:(.))?\}/gi;
    
        utils.format = function () {
            var input = arguments[0];
    
            // todo: Throw exception when we don't support the type of the argument
            var args = [];
            for (var r = 0; r < (arguments.length - 1); r++) {
                args[r] = arguments[r + 1];
            }
    
            // todo: Throw exception when this is not a string
            // todo: Use the format parameter to determine what kind of format we want to apply to the value
            return input.replace(regex, function (match, index, format) {
                var value = "";
    
                // Converts the string to number;
                index = index * 1;
    
                // todo: Throw exception if the index is greater than the length of the argument list
                if (index >= 0) {
                    value = args[index];
                }
    
                return value;
            });
        };
    
        window.utils2 = window.utils2 || utils;
    })();
    
    (function () {
        String.prototype.format14 = function () {
            var format = utils2.format;
    
            var args = [];
    
            args[0] = this;
    
            for (var r = 0; r < arguments.length; r++) {
                args[r + 1] = arguments[r];
            }
    
            return format.apply(this, args);
        };
    })();

Test runner

Ready to run.

Testing in
TestOps/sec
replace
"foo {{1}} baz {0}".replace("{1}", "foo").replace("{0}","bar");


 
ready
format1
"foo {{1}} baz {0}".format1("bar", "foo");
ready
format2
"foo {{1}} baz {0}".format2(["bar", "foo"]);
ready
format3
"foo {{1}} baz {0}".format3("bar", "foo");
ready
format4
"foo {{1}} baz {0}".format4("bar", "foo");
ready
format5
"foo {{1}} baz {0}".format5("bar", "foo");
ready
format6
"foo {{1}} baz {0}".format6("bar", "foo");
ready
format7
"foo {{1}} baz {0}".format7("bar", "foo");
ready
format8
String.prototype.format8("foo {{1}} baz {0}", "bar", "foo");
ready
format9
"foo {{1}} baz {0}".format9("bar", "foo");
ready
format10
"foo {{1}} baz {0}".format10("bar", "foo");
ready
format11
"foo {{1}} baz {0}".format11("bar", "foo");
ready
format12
"foo {{1}} baz {0}".format12("bar", "foo");
ready
format13
"foo {{1}} baz {0}".format13("bar", "foo");
ready
formatX
"foo {{1}} baz {0}".formatX("bar", "foo");
ready
utils.format
utils.format("foo {{1}} baz {0}", "bar", "foo");
ready
utils2.format
utils2.format("foo {{1}} baz {0}", "bar", "foo");
ready
format14
"foo {{1}} baz {0}".format14("bar", "foo");
ready

Revisions

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

  • Revision 1: published by Eyal Shilony on
  • Revision 2: published by Eyal Shilony on
  • Revision 3: published on
  • Revision 4: published by Eyal Shilony on
  • Revision 5: published by Eyal Shilony on