Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
insertAdjacentHTML + querySelector vs append
(version: 0)
Comparing performance of:
append vs insertAdjacentHTML
Created:
3 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<div id="test"></div>
Tests:
append
var newElement = document.createElement("div"); newElement.id = "testing" for (i = 0; i < 1000; i++) { var newElementPart = document.createElement("div"); newElementPart.classList.add("testClass"); newElement.append(newElementPart); } document.getElementById("test").append(newElement);
insertAdjacentHTML
html = "<div id='testing'>" for (i = 0; i < 1000; i++) { html += "<div class='testClass'></div>"; } html += "</div>"; test = document.getElementById("test") test.insertAdjacentHTML("beforeend", html); test.querySelector('.testClass')
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
append
insertAdjacentHTML
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):
I'll break down the provided JSON and explain what's being tested, compared options, pros and cons of those approaches, and other considerations. **Benchmark Definition** The benchmark is designed to compare the performance of two methods: 1. `insertAdjacentHTML`: This method inserts HTML content into an element without modifying the DOM. 2. `append`: This method appends a child node to an existing element in the DOM. **Options Compared** The options being compared are: * Using `insertAdjacentHTML` with `beforeend` as its argument, which will insert the HTML at the end of the element's content. * Using `append` to append a child node to the element. **Pros and Cons** 1. **InsertAdjacentHTML** * Pros: + More efficient than appending a child node using `append`, especially for large amounts of data. + Does not modify the DOM, making it more suitable for applications that require minimal DOM manipulation. * Cons: + Requires two operations (inserting HTML and setting the position), which may be slower than a single operation with `append`. 2. **Append** * Pros: + Single operation, which can be faster than multiple operations. * Cons: + May modify the DOM unnecessarily, leading to performance issues in certain scenarios. **Library Used** There is no specific library used in this benchmark. **Special JS Features/Syntax** The benchmark uses the `for` loop and `document.createElement`, `document.getElementById`, `element.insertAdjacentHTML`, and `element.querySelector` methods, which are standard JavaScript features. There are no special features or syntax mentioned. **Other Considerations** When choosing between these two approaches, consider the following factors: * **DOM Manipulation**: If you need to frequently modify the DOM, `insertAdjacentHTML` might be a better choice due to its minimal impact on the DOM. * **Performance**: For large amounts of data, `insertAdjacentHTML` can be more efficient than appending child nodes using `append`. * **Code Readability**: Some developers may find `insertAdjacentHTML` less readable or intuitive due to its abstract nature. However, it's a widely adopted and well-supported method in modern web development. **Alternatives** If you're looking for alternative approaches, consider the following: * **Template literals**: Using template literals with `innerHTML` can provide similar performance benefits as `insertAdjacentHTML`. * **DOM fragment manipulation**: Instead of appending child nodes, you can create a DOM fragment and append it to the element. This approach avoids modifying the DOM unnecessarily. * **Virtual DOM**: If you're working on a large-scale application or need to optimize rendering performance, consider using a virtual DOM library like React or Vue.js. Keep in mind that these alternatives may require more code changes and learning curve adjustments compared to `insertAdjacentHTML` and `append`.
Related benchmarks:
JS: insertBefore vs appendChild vs prepend vs insertAdjacentElement
JS: insertBefore vs appendChild vs prepend vs insertAdjacentElement vs after
innerHTML vs insertAdjacentHTML vs appendChild vs insertAdjacentElementasd
innerHTML vs insertAdjacentHTML vs appendChild vs insertAdjacentElement 2
insertAdjacentHtml vs innerHTML (no initial contents)
Comments
Confirm delete:
Do you really want to delete benchmark?