<h2><span id="num"></span> elements</h2>
<p>createDocumentFragment rendering at the end: <span id="a"></span> ms</p>
<p>innerHTML rendering at the end: <span id="b"></span> ms</p>
<p>createDocumentFragment rendering in every loop: <span id="c"></span> ms</p>
<p>innerHTML rendering in every loop: <span id="d"></span> ms</p>
<br>
<ul></ul>
var num = 100; // Please don't use more than 5000 if don't want to crash your browser
var text = "Any text you want"; // this will not have any influence in our test
var i, start_date;
var $ul = document.getElementsByTagName("ul")[0];
document.getElementById("num").textContent = num;
// createDocumentFragment rendering at the end
var li, div, p;
var fragment = document.createDocumentFragment();
start_date = performance.now();
for (i = 0; i < num; i++) {
li = document.createElement("li");
li.setAttribute("class", "element");
li.setAttribute("id", "element" + i);
div = document.createElement("div");
div.setAttribute("class", "icon");
li.appendChild(div);
p = document.createElement("p");
p.setAttribute("class", "serialcode");
p.textContent = text;
div.appendChild(p);
fragment.appendChild(li);
}
$ul.appendChild(fragment);
document.getElementById("a").textContent = performance.now() - start_date;
$ul.innerHTML = "";
// inncerHTML rendering at the end
var html = "";
start_date = performance.now();
for (i = 0; i < num; i++) {
html +=
'<li class="element" id="element' + i + '">' +
'<div class="icon">' +
'<p class="serialcode">' + text + '</p>' +
'</div></li>';
}
$ul.innerHTML = html;
document.getElementById("b").textContent = performance.now() - start_date;
$ul.innerHTML = "";
// createDocumentFragment rendering in every loop
fragment = document.createDocumentFragment();
start_date = performance.now();
for (i = 0; i < num; i++) {
li = document.createElement("li");
li.setAttribute("class", "element");
li.setAttribute("id", "element" + i);
div = document.createElement("div");
div.setAttribute("class", "icon");
li.appendChild(div);
p = document.createElement("p");
p.setAttribute("class", "serialcode");
p.textContent = text;
div.appendChild(p);
fragment.appendChild(li);
$ul.appendChild(fragment);
}
document.getElementById("c").textContent = performance.now() - start_date;
$ul.innerHTML =...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.