string building techniques (v2)

Revision 2 of this benchmark created by Maurice Rabb on


Preparation HTML

<script>
        var String_concat = String.prototype.concat;
        var data = "apple orange banana blueberry pumelo grapefruit pear acai pineapple cherry durian";
        data = data.split(" ");
        data = data.concat(data, data, data, data, data, data, data);
        
        // http://jsperf.com/join-vs-concat-dm
        // http://jsperf.com/string-building
        // http://jsperf.com/string-building-techniques
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
array push and join
        var index, count, parts;
        index = -1;
        count = data.length;
        parts = ["Thing:"];
        while (++index < count) {
                parts.push(data[index]);
        }
        return parts.join(" ");
ready
array index and join
        var index, count, parts;
        index = -1;
        count = data.length;
        parts = ["Thing:"];
        while (++index < count) {
                parts[index] = data[index];
        }
        return parts.join(" ");
ready
concat operator
        var index, count, string;
        index = -1;
        count = data.length;
        string = "Thing:";
        while (++index < count) {
                string += data[index];
        }
        return string;
ready
concat method
        var index, count, string;
        index = -1;
        count = data.length;
        string = "Thing:";
        while (++index < count) {
                string.concat(data[index]);
        }
        return string;
ready
concat method with multiple args
        return String_concat.apply("Thing:", data);
ready

Revisions

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

  • Revision 2: published by Maurice Rabb on