Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Vanilla appendChild vs innerHTML
(version: 0)
Comparing performance of:
appendChild vs innerHTML
Created:
5 years ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<ul id="itemList"></ul>
Script Preparation code:
const itemList = document.getElementById("itemList");
Tests:
appendChild
for(let i = 0; i < 10; i++) { const node = document.createElement("li"); node.innerHTML = "Item nr: " + i; itemList.appendChild(node); }
innerHTML
let newItems = ""; for(let i = 0; i < 10; i++) { newItems += "<li>Item nr: " + i + "</li>"; } itemList.innerHTML = newItems;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
appendChild
innerHTML
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 definition and test cases to understand what is being tested. **Benchmark Definition** The benchmark compares two approaches for appending content to an HTML element: `innerHTML` and `appendChild`. The test aims to determine which approach is faster, more efficient, and better suited for a specific use case. **Script Preparation Code** The script preparation code creates a constant reference to an HTML element with the id "itemList". This element will be used as the target for appending content. **Html Preparation Code** The HTML preparation code generates a basic HTML structure, including an unordered list (`<ul>`) with the id "itemList", which matches the element referenced in the script preparation code. **Individual Test Cases** There are two test cases: 1. **appendChild**: This test case uses a traditional `for` loop to create 10 new list items and appends them to the target element using the `appendChild` method. 2. **innerHTML**: This test case creates a string containing the HTML for 10 new list items and assigns it to the `innerHTML` property of the target element. **Libraries Used** There is no explicit library mentioned in the benchmark definition or individual test cases. However, it's worth noting that both approaches rely on the built-in JavaScript DOM (Document Object Model) APIs. **Special JS Features/Syntax** None are explicitly mentioned in this benchmark. However, some modern browsers may utilize features like `const` and template literals (`\`${...}\``), which are used in the script preparation code. These features are not specific to JavaScript performance testing but rather a reflection of modern coding practices. **Pros and Cons of Different Approaches** 1. **innerHTML**: Pros: * Faster, as it involves less overhead compared to `appendChild`. * More convenient for simple cases where you need to set the entire HTML content. Cons: * Can lead to performance issues if used excessively or with complex layouts. * May not be suitable for dynamic or ever-changing content. 2. **appendChild**: Pros: * Better suited for dynamic or ever-changing content, as it allows for individual element manipulation. * More flexible and efficient for complex layouts or frequent changes. Cons: * Slower compared to `innerHTML` due to the overhead of creating and manipulating elements individually. **Other Alternatives** If you're looking for alternative approaches, consider: 1. **insertAdjacentHTML()**: A newer method that's faster than `innerHTML` but slower than `appendChild`. It's designed specifically for inserting HTML content at a specific point in the DOM. 2. **DOM Manipulation using Node.js APIs**: You can use native Node.js APIs like `child_process` or `node-html-parser` to manipulate the DOM, which might offer better performance compared to JavaScript's built-in DOM methods. Keep in mind that these alternatives are not necessarily designed for benchmarking purposes and may have their own trade-offs.
Related benchmarks:
JS: append vs appendChild
ParentNode.append() vs Node.appendChild() for adding a couple elements
Array.from vs Spread with Node List DOM
Array.from vs Spread with Node List DOM More
Comments
Confirm delete:
Do you really want to delete benchmark?