Building string via loop vs. join

JavaScript performance comparison

Test case created

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var result = "";
    var array = ["alpha", "beta", "gamma", "delta", "epsilon", "pi", "mu", "nu", "omicron", "lambda"];
};
</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
Loop
for (var i = 0; i < array.length; i++) {
   results += (results == "" ? "" : ",") + array[i];
}
pending…
Join
var outputArray = [];
outputArray.push(array[0]);
outputArray.push(array[1]);
outputArray.push(array[2]);
outputArray.push(array[3]);
outputArray.push(array[4]);
outputArray.push(array[5]);
outputArray.push(array[6]);
outputArray.push(array[7]);
outputArray.push(array[8]);
outputArray.push(array[9]);
var results = outputArray.join(",")
 
pending…
Loop 2
for (var i in array) {
   results += (results == "" ? "" : ",") + array[i];
}
pending…
Join 2
var outputArray = [];
outputArray[outputArray.length] = array[0];
outputArray[outputArray.length] = array[1];
outputArray[outputArray.length] = array[2];
outputArray[outputArray.length] = array[3];
outputArray[outputArray.length] = array[4];
outputArray[outputArray.length] = array[5];
outputArray[outputArray.length] = array[6];
outputArray[outputArray.length] = array[7];
outputArray[outputArray.length] = array[8];
outputArray[outputArray.length] = array[9];
var results = outputArray.join(",")
 
pending…
Loop 3
var i = 0;
while (i < array.length) {
   results += (results == "" ? "" : ",") + array[i++];
}
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