Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Creating/adding html element performance (5 elements) v2
(version: 0)
Comparing performance of:
Pure JavaScript vs Strings / append vs Strings array / append
Created:
4 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script type="text/javascript" src="https://code.jquery.com/jquery-3.2.1.min.js"></script> <div id="test"></div>
Tests:
Pure JavaScript
var c = document.createDocumentFragment(); var elem = document.getElementById("test"); for (var i=0; i<5; i++) { const inputElem = document.createElement("input"); inputElem.id = "inputTest" + i; inputElem.name = "inputTest"; inputElem.type = "radio"; inputElem.value = "value"; const labelElem = document.createElement("label"); labelElem.htmlFor = "inputTest" + i; labelElem.innerText = "inputTest" + i; labelElem.setAttribute("style", "color: " + "#FF0000"); const container = document.createElement("label"); container.appendChild(inputElem); container.appendChild(labelElem); c.appendChild(container); elem.appendChild(c); }
Strings / append
var s = ""; var test = $("#test"); for (var i=0; i<5; i++) { s += "<label><input id='inputTest" + i + "' type='radio' name='inputTest' value='value'><label for='inputTest" + i + "' style='color: #FF0000'>inputTest" + i + "</label></label>"; test.append(s); }
Strings array / append
var a = []; var test = $("#test"); for (var i=0; i<5; i++) { a.push("<label><input id='inputTest" + i + "' type='radio' name='inputTest' value='value'><label for='inputTest" + i + "' style='color: #FF0000'>inputTest" + i + "</label></label>"); test.append(a.join("")); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Pure JavaScript
Strings / append
Strings array / append
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):
Let's break down the provided benchmark and explain what is being tested. **Benchmark Overview** The benchmark measures the performance of creating and adding HTML elements to an existing DOM element. The test creates 5 radio input fields with labels, appends them to a container element, and then appends the container to another parent element. **Test Cases** There are three test cases: 1. **Pure JavaScript**: This test case uses only JavaScript libraries and does not use any external resources. It creates an array of HTML strings using `document.createElement` and ` document.appendChild`. The test then appends this array of HTML strings to the `#test` element. 2. **Strings / append**: This test case uses jQuery, a popular JavaScript library for DOM manipulation. It creates an empty string and appends it to the `#test` element using jQuery's `append` method. Inside the string, it creates individual radio input fields and labels using `document.createElement`, `innerHTML`, and `style`. 3. **Strings array / append**: Similar to the previous test case, but instead of creating a single string, this test case pushes an array of HTML strings to the `#test` element using jQuery's `append` method. **Options Compared** The three test cases compare different approaches to creating and appending HTML elements: * Using JavaScript libraries (Pure JavaScript) vs. using external resources like jQuery (Strings / append). * Creating individual HTML elements using `document.createElement` vs. concatenating strings with innerHTML (Strings / append). **Pros and Cons** Here's a brief summary of the pros and cons of each approach: 1. **Pure JavaScript**: * Pros: No external dependencies, simple implementation. * Cons: May be slower due to the overhead of creating individual elements and appending them multiple times. 2. **Strings / append**: (using jQuery) * Pros: Faster since it uses a library that optimizes DOM manipulation, reducing the number of elements created and appended. * Cons: Requires including an external library, may have additional dependencies. 3. **Strings array / append**: * Pros: Similar performance to Strings / append, with the added benefit of using an array data structure for better memory management. * Cons: May be slower due to the overhead of creating individual elements and pushing them to an array. **Other Considerations** When interpreting benchmark results, it's essential to consider factors like: * Browser and device differences * Network conditions (e.g., latency, bandwidth) * System resources (e.g., CPU, memory) Keep in mind that these factors can significantly impact performance. It's also worth noting that the test cases are relatively simple, so the results may not accurately reflect real-world scenarios. **Alternatives** Other approaches to creating and appending HTML elements could be considered: * Using a templating engine like Handlebars or Mustache * Utilizing a library like React or Angular for component-based development * Leveraging CSS-in-JS solutions like Styled Components or Emotion However, these alternatives might introduce additional complexity and overhead, which may not be suitable for all use cases.
Related benchmarks:
HTML insertion
reparsing jQuery speed test
jQuery by id vs Document.getElementById
Creating/adding html element performance (100 elements)
createElement vs JQuery $
Comments
Confirm delete:
Do you really want to delete benchmark?