img vs innerhtml

JavaScript performance comparison

Test case created by Prashanth

Info

I want to show new Img() is slower than innerHTML.

Preparation code

<div id="container"></div>
<div id="container1"></div>

Preparation code output

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
inject by creating img
var container = document.getElementById('container')

for (var i = 0; i < 10; i++) {
  var img = new Image();
  img.src = i;
  img.alt = i;
  container.appendChild(img);
}
 
pending…
innerHTML
var container = document.getElementById('container1');
var html = "";

for (var i = 0; i < 10; i++) {
  html += "<img src='"+i+"' alt='"+i+"'/>"
}

container.innerHTML = html;
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