<table id="tbl">
<tbody></tbody>
</table>
var tbody = tbl.tBodies[0];
var data = { one: 'one', two: 'two', three: 'three', four: 'four', five: 'five' };
var row = tbl.insertRow();
var cell = row.insertCell();
var textnode = document.createTextNode(data.one);
cell.appendChild(textnode);
cell = row.insertCell();
textnode = document.createTextNode(data.two);
cell.appendChild(textnode);
cell = row.insertCell();
textnode = document.createTextNode(data.three);
cell.appendChild(textnode);
cell = row.insertCell();
textnode = document.createTextNode(data.four);
cell.appendChild(textnode);
cell = row.insertCell();
textnode = document.createTextNode(data.five);
cell.appendChild(textnode);
var html = '<tr>';
html += '<td>'+data.one+'</td>';
html += '<td>'+data.two+'</td>';
html += '<td>'+data.three+'</td>';
html += '<td>'+data.four+'</td>';
html += '<td>'+data.five+'</td>';
html += '</tr>';
tbody.insertAdjacentHTML('beforeend', html);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
insertRow() | |
insertAdjacentHTML() |
Test name | Executions per second |
---|---|
insertRow() | 65856.8 Ops/sec |
insertAdjacentHTML() | 57502.6 Ops/sec |
Benchmark Overview
The provided JSON represents a JavaScript microbenchmark, specifically measuring the performance of two approaches for inserting table rows: insertRow()
and insertAdjacentHTML()
. The benchmark is designed to test vanilla JS versions without using jQuery.
Test Cases
There are two individual test cases:
insertRow()
: This test case manually inserts rows into a table body using the insertRow()
method, followed by appending text nodes to each cell.insertAdjacentHTML()
: This test case uses insertAdjacentHTML()
to insert a string containing HTML elements (in this case, table rows) and appends it to the end of the table body.Comparison
The two approaches are compared in terms of performance, measured by the number of executions per second. The benchmark measures how fast each approach can execute.
Options Compared
insertRow()
: A method that creates a new row element and inserts it into the table body.insertAdjacentHTML()
: A method that allows inserting HTML elements at a specified position relative to an element.Pros and Cons of Each Approach
insertRow()
Pros:
insertAdjacentHTML()
.Cons:
insertAdjacentHTML()
Pros:
Cons:
Library/Functionality Used
The benchmark uses the insertAdjacentHTML()
method from the JavaScript DOM API. This method allows inserting HTML elements at a specified position relative to an element.
Special JS Feature/Syntax
None mentioned in the provided benchmark, but it's essential to note that some newer browsers or versions may optimize or handle certain features differently, affecting performance comparisons.
Alternatives
Some potential alternatives for building table rows include:
createElement()
and setting attributes, then appending elements to the table body.Keep in mind that each approach has its trade-offs and may better suit specific use cases.