Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Dynamic Create of Table: createElement() vs append HTML
(version: 3)
Comparing performance of:
createElement vs Append HTML vs Combination
Created:
9 years ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script> <table id="tbl"> <tbody></tbody> </table>
Script Preparation code:
var table = $('#tbl'), tbody = table.find('tbody'); var data = { one: 'one', two: 'two', three: 'three', four: 'four', five: 'five' };
Tests:
createElement
var tr = $(document.createElement('tr')); var td = $(document.createElement('td')); td.text(data.one).append(td); td = $(document.createElement('td')); td.text(data.two).append(td); var td = $(document.createElement('td')); td.text(data.three).append(td); var td = $(document.createElement('td')); td.text(data.four).append(td); var td = $(document.createElement('td')); td.text(data.five).append(td); tbody.append(tr);
Append HTML
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.append(html);
Combination
var tr = $(document.createElement('tr')); tr.append('<td>'+data.one+'</td>'); tr.append('<td>'+data.two+'</td>'); tr.append('<td>'+data.three+'</td>'); tr.append('<td>'+data.four+'</td>'); tr.append('<td>'+data.five+'</td>'); tbody.append(tr);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
createElement
Append HTML
Combination
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Overview of the Benchmark** The provided JSON represents a JavaScript microbenchmarking test case on the MeasureThat.net website. The test measures the performance of creating and appending table rows in different ways. **What is being tested?** Three individual test cases are compared: 1. **createElement**: Creates a new table row (`tr`) using `document.createElement('tr')`, then appends child elements to it. 2. **Append HTML**: Appends an HTML string directly to the `tbody` element, which contains the table rows. 3. **Combination**: A combination of creating a new table row and appending its child elements using the `append()` method. **Options compared** The three test cases compare different approaches to create and append table rows: * `createElement`: Creates a new table row and appends child elements manually. * `Append HTML`: Appends an entire HTML string to the `tbody` element, which includes the table row and its child elements. * **Combination**: Uses both `createElement` and `append()` methods to create and append table rows. **Pros and Cons of each approach** 1. **createElement**: * Pros: More control over the structure of the table row. * Cons: Requires more code and can be slower due to the overhead of creating a new element. 2. **Append HTML**: * Pros: Faster and more concise, as it avoids creating a new element. * Cons: Less control over the structure of the table row, and may lead to inconsistent output if not carefully crafted. 3. **Combination**: * Pros: Balances between control and conciseness, using both methods to create and append table rows. * Cons: May be slower due to the overhead of creating a new element. **Library usage** The test case uses jQuery, which is a popular JavaScript library for DOM manipulation. Specifically, it uses `$(document.createElement('tr'))` to create a new table row and `$(`tbody.append()` to append its child elements. **Special JS feature or syntax** This benchmark does not use any special JavaScript features or syntax that are specific to certain browsers or versions. **Other alternatives** There are other ways to create and append table rows, such as: * Using `innerHTML` instead of `createElement` and `appendChild`. * Utilizing the `DOMParser` API to parse an HTML string and create a new element. * Leveraging modern JavaScript features like template literals or string interpolation. However, these alternatives may not be relevant to this specific benchmark, which focuses on comparing different approaches to creating and appending table rows using jQuery.
Related benchmarks:
jquerySelector vs getElementById and getElementsByTagName
Move Element to another 2
Dynamic Create of Table: insertRow() vs appending HTML
Add rows to Table: insertRow() vs appending innerHTML vs insertAdjacentHTML
Comments
Confirm delete:
Do you really want to delete benchmark?