I have perfomance issues when inserting data into the dom.
Insertions + jQuery mobile enhancement done on pagecontainerbeforeshow() event, jQuery mobile version 1.4.2.
I have tried to see which would be the fastest approach by comparing three simplified versions of what I want to do:
jQuery approach :
for(var i=0;i<2000;++i){
$('<div>').attr({'data-role':'collapsible','id':'asdf'+i+''}).html('<h2>asdf</h2>').appendTo("#manage_content");
$('<ul>').attr({'data-role':'listview'}).html('<li>bit</li>').appendTo('#asdf'+i+'');
}
$('#manage').trigger('create');
Pure js, creating all nodes :
var d=document.createDocumentFragment();
var title,listitem,list;
var coll=new Array();
for(var i=0;i<2000;++i){
coll[i]=document.createElement('div');
coll[i].setAttribute("data-role", "collapsible");
title = document.createElement('h2');
title.innerHTML='asdf';
coll[i].appendChild(title);
list=document.createElement('ul');
list.setAttribute("data-role","listview");
listitem = document.createElement('li');
listitem.innerHTML='bit';
list.appendChild(listitem);
coll[i].appendChild(list);
d.appendChild(coll[i]);
}
document.getElementById("manage_content").appendChild(d);
$('#manage').trigger('create');
jQuery with big string :
var html='';
for(var i=0;i<2000;++i){
html+='<div data-role="collapsible"><h2>asdf<h2><ul data-role="listview"><li>bit</li></ul></div>';
}
$('#manage_content').append(html);
$('#manage').trigger('create');
To my surprise, the three ways of doing this give same result (around 7seconds execution time...)
Am I doing any of the ways wrong? Is there a better way?
I have seen lots of questions related to this topic but mostly outdated or stating that the pure javascript should be faster, which is not the case for me.
Without jQuery enhancement :
So pure javascript with document fragments is the best even though it is horrible to read/write x_x
With jQuery enhancement :
Test here (credits to @Omar)