Supplant, concat, array-join

JavaScript performance comparison

Test case created

Info

Test of string construction by 3 techniques: - Basic concatenation - Using "supplant" and a template - Using array join.

I've heard different stories about these techniques, and how recent Javascript efficiencies have changed things. Thought I'd build a test to check it all out.

Preparation code

<script type="text/javascript">
String.prototype.supplant = function (o) {
    return this.replace(
        /\{([^{}]*)\}/g,
        function (a, b) {
            var r = o[b];
            return typeof r === 'string' || typeof r === 'number' ? r : a;
        }
    );
};

var count = 12;
var base_template = "{a} ";
var template = "";

var letter = 'b';
var dict = { a: 'b' };

for (var n = 0; n < count; n++) {
    template = template + base_template;
}
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
supplant from remedial Javascript
supplant_result = template.supplant(dict);
 
pending…
string concat
var concat_result = "";
for (var n = 0; n < count; n++) {
    concat_result = concat_result + letter;
}
 
pending…
array join
array_to_join = [];
for (var n = 0; n < count; n++) {
    array_to_join.push(letter);
}
join_result = array_to_join.join("");
 
pending…

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

Compare results of other browsers

0 comments

Add a comment