Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
replaceChild vs replaceChildren vs documentFragment 2
(version: 0)
Comparing performance of:
replaceChild vs replaceChildren vs append documentFragment
Created:
3 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<div id="container"></div>
Script Preparation code:
var elements = [document.createElement('div'), document.createElement('div')]; var fragment = document.createDocumentFragment(); var container = document.getElementById('container'); elements.forEach(element => fragment.appendChild(element)); container.appendChild(fragment);
Tests:
replaceChild
var changeIndex = 1; elements[changeIndex] = document.createElement('div'); container.replaceChild(elements[changeIndex], container.children[changeIndex]);
replaceChildren
var changeIndex = 1; elements[changeIndex] = document.createElement('div'); container.replaceChildren(elements)
append documentFragment
var changeIndex = 1; var newChildren = document.createDocumentFragment(); elements[changeIndex] = document.createElement('div'); elements.forEach(element => newChildren.appendChild(element)); container.replaceChildren(newChildren)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
replaceChild
replaceChildren
append documentFragment
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:126.0) Gecko/20100101 Firefox/126.0
Browser/OS:
Firefox 126 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
replaceChild
1434729.0 Ops/sec
replaceChildren
575702.9 Ops/sec
append documentFragment
635970.6 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down what's being tested in this benchmark. **Benchmark Goal** The benchmark measures the performance difference between three approaches for replacing or appending child elements to an HTML container: 1. `replaceChild` 2. `replaceChildren` 3. Using a `documentFragment` to append children **Options Compared** Each option is compared in terms of execution speed (measured as executions per second). ### 1. replaceChild `replaceChild` replaces the specified child element with a new one, and removes the old one from the DOM. Pros: * Simple and straightforward implementation * Fast and efficient, since it only involves updating the DOM tree Cons: * May lead to unnecessary DOM reflows or repainting, especially if the replaced element has styles or attributes that need to be reapplied. * Can result in a higher number of DOM mutations (i.e., changes to the DOM tree) compared to other options. ### 2. replaceChildren `replaceChildren` replaces all child elements with a new set of children. Pros: * May reduce the number of DOM mutations, since it only involves replacing the existing children and adding new ones. * Can be more efficient if the replaced children have similar structure or content. Cons: * May lead to slower performance if the replaced children are complex or have many attributes. * Requires careful consideration of the new children's structure and content to ensure optimal performance. ### 3. Append documentFragment Using a `documentFragment` to append children creates a new container element that holds all the child elements, which can then be appended to the parent element in one operation. Pros: * Reduces DOM mutations by creating a new container element. * Can improve performance for large numbers of children or complex child elements. * Allows for more control over the order and structure of the append operation. Cons: * Requires additional memory allocation for the `documentFragment` element. * May lead to slower performance if the `documentFragment` is not properly optimized (e.g., by using an efficient fragment creation algorithm). **Libraries Used** None are explicitly mentioned in the provided benchmark definition. However, it's worth noting that some implementations of `replaceChild` and `replaceChildren` might rely on external libraries or frameworks for DOM manipulation. **Special JS Features/Syntax** There is no special JavaScript feature or syntax used in this benchmark. The code is standard JavaScript, with no references to advanced features like async/await, promises, or modern ES6+ syntax. **Other Alternatives** Some alternative approaches to replacing or appending child elements include: * Using `insertBefore` or `insertAfter` instead of `replaceChild` * Creating a new container element and appending the children manually * Using a library like jQuery for DOM manipulation (although this is not explicitly mentioned in the benchmark definition) Keep in mind that these alternatives might have different performance characteristics, trade-offs, or use cases compared to the options being tested in this benchmark.
Related benchmarks:
Cherrypick replace child vs appending a document fragment
replaceChild vs replaceChildren vs documentFragment
replaceWith vs replaceChild without iframe
replaceChild vs replaceChildren vs documentFragment 3
Comments
Confirm delete:
Do you really want to delete benchmark?