Appending HTML element with attributes

JavaScript performance comparison

Revision 7 of this test case created by jason

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…
Actual DOM Scripting
var div = document.getElementById("screenshots"),
    anchor, img;
if (div) {
  anchor = document.createElement("div");
  anchor.title = title;
  img = document.createElement("iframe");
  img.src = 'about:blank';
  img.width = 200;
  img.height = 200;
  anchor.appendChild(img);
  div.appendChild(anchor);
}
pending…
insertAdjacentHTML
document.getElementById("screenshots").insertAdjacentHTML("beforeend", '<div title="' + title + '"><iframe src="about:blank" width="200px" height="200px"></iframe></div>');
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.

0 Comments