string concatenation or adding dom (v7)

Revision 7 of this benchmark created by Erik Vorhes on


Description

Updated test 2, because it didn't include a ul element.

Preparation HTML

<div id='container'>
</div>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>

Setup

var arr = [],
        urlbase = "http://www.somegallery.fr/image/",
        $container = $("#container");
    
    for (var i = 0, l = 100; i < l; i++) {
       arr.push(urlbase + i);
    }

Teardown


    $container.empty();
  

Test runner

Ready to run.

Testing in
TestOps/sec
append on dom
var $ul = $("<ul>");
$container.append($ul);

    $.each(arr, function(count, item) {
        var newImg = '<li><img src="'+item+'"></li>';
        $ul.append(newImg);
    });
ready
string concatenation
var tmp = '<ul>'; 
$.each(arr, function(count, item) {
    tmp += '<li><img src="'+item+'"></li>';
});
tmp += '</ul>';
$container.append(tmp);
ready
append on unattached dom with string concatenation
var $ul = $("<ul>");

    $.each(arr, function(count, item) {
        var newImg = '<li><img src="'+item+'"></li>';
        $ul.append(newImg);
    });

$container.append($ul);
 
ready
USE THIS: append on unattached dom without string concatenation
var $ul = $("<ul>");

    $.each(arr, function(count, item) {
        var $img = $('<img>').attr('src', item);
        var $li = $("<li>").append($img);
        $ul.append($li);
    });

$container.append($ul);
 
ready

Revisions

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