Appending HTML element with attributes

JavaScript performance comparison

Revision 3 of this test case created by Paul Grenier

Preparation code

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js">
</script>
<div id="screenshots">
</div>
      
<script>
Benchmark.prototype.setup = function() {
  var link = "something.html";
  var title = "Some Title";
  var el = "someImg.png";

};

Benchmark.prototype.teardown = function() {
  $("#screenshots").empty();

};
</script>

Preparation code output

<div id="screenshots"> </div>

Test runner

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

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
Original Code
$('#screenshots').append($('<a href="' + link + '" title="' + title + '"><img src="' + el + '" alt="" width="200px" height="200px"></a>'));
pending…
DOM scripting
$("#screenshots").append(
$("<a>").attr({
  'href': link,
  'title': title
}).append(
$("<img>").attr({
  'src': el,
  'alt': '',
  'width': '200px',
  'height': '200px'
})));
pending…
getElementById append
$(document.getElementById("screenshots")).append('<a href="' + link + '" title="' + title + '"><img src="' + el + '" alt="" width="200px" height="200px"></a>');
pending…
no jQuery
document.getElementById("screenshots").insertAdjacentHTML("beforeend", '<a href="' + link + '" title="' + title + '"><img src="' + el + '" alt="" width="200px" height="200px"></a>');
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.

1 Comment

Will Klein commented :

Wunderbar! Thanks Paul, I agree that your no jQuery case is most efficient. I was actually after that but was too tired last night to look it up.