Building a List via Array

JavaScript performance comparison

Test case created by Dan Mathisen

Info

Comparing looping through an array vs array joining to build a HTML list.

Preparation code

<script>
    var arr = ["item1", "item2", "item3", "item4", "item5", "item6", "item7"];
</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
Build with for loop
var html = '<ul>',
    i = 0,
    len = arr.length;

for (i; i < len; i++) {
    html += '<li>' + arr[i]; + '</li>';
}

html = "</ul>";

console.log(html);
pending…
Build with join
var html = '<ul><li>' + arr.join('</li><li>') + '</li></ul>';
console.log(html);
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:

0 comments

Add a comment