Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
createElement vs createDocumentFragment @oneook 3
(version: 0)
Comparing performance of: createElement vs createDocumentFragment
Comparing performance of:
createElement vs createDocumentFragment
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
document.body.innerHTML = "";
Tests:
createElement
var root = document.createElement("div"); for (var i = 0; i < 10000; i++) { var e = document.createElement("span"); e.innerText = i + " text"; root.appendChild(e); } document.body.appendChild(root);
createDocumentFragment
var frag = document.createDocumentFragment(); for (var i = 0; i < 10000; i++) { var e = document.createElement("span"); e.innerText = i + " text"; frag.appendChild(e); } document.body.appendChild(frag);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
createElement
createDocumentFragment
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 dive into the world of JavaScript microbenchmarks. **What is being tested?** The provided benchmark tests two approaches to append elements to a document: `createElement` and `createDocumentFragment`. The test creates 10,000 HTML elements (span) with incrementing text content and appends them to a container element using each approach. The benchmark measures the performance of these two approaches in terms of executions per second. **Options compared** The main difference between the two approaches is how they handle the creation and disposal of temporary objects: 1. `createElement`: A new HTML element (`span` in this case) is created on every iteration, which can lead to: * Memory allocation and deallocation for each element. * Potential DOM tree size increase due to multiple elements being added at once. 2. `createDocumentFragment`: A temporary fragment object is created, which contains the appended elements, reducing memory allocation and deallocation overhead. However, it still involves: * Creating a new fragment object on every iteration. **Pros and Cons of each approach** `createElement`: Pros: * Simple to implement * Does not require any special library or knowledge Cons: * Can lead to increased memory allocation and deallocation due to the creation of multiple elements. * May result in slower performance due to DOM tree size increase. `createDocumentFragment`: Pros: * Reduces memory allocation and deallocation overhead by reusing a single fragment object. * Can be faster for large numbers of appended elements. Cons: * Requires creating a new fragment object on every iteration, which can lead to slower performance due to the creation and disposal of temporary objects. * May require additional library or knowledge (in this case, `createDocumentFragment` is a part of the DOM API). **Library and special JS features** The benchmark uses the following library: 1. `document.createDocumentFragment()`: This method is part of the DOM API and creates a new fragment object that can be used to append elements without the overhead of creating multiple elements. There are no special JavaScript features used in this benchmark beyond what's standard for vanilla JavaScript and the DOM API. **Other alternatives** Some alternative approaches could include: 1. Using `appendChild` with a single element: Instead of appending individual elements, you could create a single container element (e.g., a `div`) and append all elements to it. 2. Using `innerHTML`: You could set the `innerHTML` property of an element directly instead of using `appendChild`. 3. Using a library like React or Angular: These frameworks provide optimized rendering and appending mechanisms that might outperform simple DOM manipulation. However, these alternatives would likely require significant changes to your codebase and may not be suitable for all use cases. That's a wrap on this benchmark explanation!
Related benchmarks:
createTextNode vs innerHTML vs innerText
createElement vs createDocumentFragment @oneook
createElement vs createDocumentFragment @oneook 2
createTextNode vs textContent vs innerText vs innerHTML (for reading)
Comments
Confirm delete:
Do you really want to delete benchmark?