String.Format underscore vs hot (v4)

Revision 4 of this benchmark created on


Description

Added test format without using RegEx

Preparation HTML

<script src="http://documentcloud.github.com/underscore/underscore-min.js"></script>

<script>
  var hot = {
  rx: /\{(\S+)\}/g
};
  hot.format = function (string, context) {
                return string.replace(this.rx, function (str, key) { return context[key] });
        };
  
  _.templateSettings = {
    interpolate : /\{(.+?)\}/g
  };
hot.formatWithoutRegEx=function(string, parameters){
            var parametersKeys,
                key,
                i;
                parametersKeys = Object.keys(parameters);
                for(i = 0; i < parametersKeys.length; i++){
                    key = parametersKeys[i];
                    string = string.replace("{" + key + "}", parameters[key]);
                }
            return string;
        };
</script>

Setup

var t1 = _.template( "{x}" ),
    t2 = _.template("{x} {y}"),
    t3 = _.template("{x} {y} {z}");

Test runner

Ready to run.

Testing in
TestOps/sec
Underscore
t1({
    x : 1
});


t2({
    x : 1,
    y : 2
});

t3({
    x : 1,
    y : 2,
    z : 3
});
ready
Hot
hot.format("{x}", {
    x : 1
});

hot.format("{x}, {y}", {
    x : 1,
    y : 2
});

hot.format("{x}, {y}, {z}", {
    x : 1,
    y : 2,
    z : 3
});
ready
Hot Without RegEx
hot.format("{x}", {
    x : 1
});

hot.formatWithoutRegEx("{x}, {y}", {
    x : 1,
    y : 2
});

hot.formatWithoutRegEx("{x}, {y}, {z}", {
    x : 1,
    y : 2,
    z : 3
});
ready

Revisions

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