different-format-imp

Benchmark created by Eyal Shilony on


Setup

(function () {
        var isDigit = /\d/;
        var toNumber = Number;
        var pow = Math.pow;
    
        String.prototype.format1 = 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.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 = "";
    
                var isDigit = /\d/;
                var toNumber = Number;
                var pow = Math.pow;
    
            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;
        };
    })();

Test runner

Ready to run.

Testing in
TestOps/sec
format1
"foo {{1}} baz {0}".format1("bar", "foo");
ready
format2
"foo {{1}} baz {0}".format2("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