Best way to create large static DOM elements in JavaScript?

Detailed analysis of 3 commons ways of creating DOM in JS and the best approach.

I will provide 3 ways to create large DOM and their pros and cons, and of-course the most optimized way for large DOM creation and why.
Bottom line is while creating DOM in js, native JS and DOM methods are your friend, don’t use Jquery unless there is no other way(which is unlikely).

Test Data for comparison: Created 400 rows with 5 columns and appended to DOM.
testData is list of objects that you get from backend in json form to create table.

Attached Execution time test result snapshot for different browsersenter image description here
HTML

<div id="employeeListContainer1"></div>
<table id="employeeList2">
    <thead>
        <tr>
            <th>First Name</th>
            <th>Last Name</th>
            <th>Title</th>
            <th>ID</th>
            <th>Department</th>
        </tr>
    </thead>
</table>

1st way : String Concatenation (Most Optimized way in terms of performance across browsers)

var tableElementContainer1 = document.getElementById("employeeListContainer1");
var temptableHolder="<table><thead><tr><th>First Name</th><th>Last Name</th><th>Title</th><th>ID</th><th>Department</th></tr></thead><tbody>";

for(var i=0,len=testData.length; i<len; i++){
    temptableHolder  += '<tr><td>' + testData[i].firstName + '</td><td>' + testData[i].lastName + '</td><td>' + testData[i].title
        + '</td><td>' + testData[i].id + '</td><td>'  + testData[i].department +  '</td></tr>';
}

temptableHolder += '</tbody></table>';
tableElementContainer1.innerHTML  = temptableHolder ;

Pros:

  • Fastest execution time across Firefox/Chrome/IE/Safari (3 to 5 millisec across browsers). Measured via both performance.now() and console.time() APIs.

Cons:

  • When number of columns are more and you need to set lot of attributes then working with strings can get little difficult and less main tenable.

2nd way: Native Js document.createElement()(This is 2nd best approach in terms of performance across browsers)

var tableBody = document.createElement('tbody');
var tableElement2 = document.getElementById("employeeList2");

for(var i=0,len=testData.length; i<len; i++){
    tableRow = document.createElement("tr");
    
    for(var k in testData[i]){
        rowCell = document.createElement("td");
        rowCell.appendChild(document.createTextNode(testData[i][k]));
        tableRow.appendChild(rowCell);
    }
    
    tableBody.appendChild(tableRow);
}

tableElement2.appendChild(tableBody);

Pros:

  • 2nd fastest execution time across Firefox/Chrome/Safari (5 to 12 millisec across browsers). Measured via both performance.now() and console.time() APIs.
  • More main tenable than 1st Approach

Cons:

  • Execution time is more in IE browsers, 90+ millsec

3rd Way: Using Jquery to create DOM (My advise is don’t use it)

var tableBody = $('<tbody></tbody>');
var tableElement2 = document.getElementById("employeeList2");

for(var i=0,len=testData.length; i<len; i++){
    tableRow = $("<tr></tr>");
    
    for(var k in testData[i]){
        rowCell = $("<td></td>");
        rowCell.append(testData[i][k]);
        tableRow.append(rowCell);
    }
    
    tableBody.append(tableRow);
}

tableElement2.append(tableBody);

Pros:

  • Easy to add attributes/class/styles on elements and is easy to read and main tenable.

Cons:

  • Worst execution time across all browsers (220 ms to 330 ms), slowest numbers are in IE

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)