Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
DOM: Append Performance
(version: 4)
Document Fragment vs. String Concatenation
Comparing performance of:
Document Fragment vs String Concatenation
Created:
8 years ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<div id="content"></div> <script src="//code.jquery.com/jquery-3.3.1.min.js"></script>
Script Preparation code:
var myArray = ["Hello", "world", "!", "This", "is", "a", "benchmark", "!"];
Tests:
Document Fragment
// Appending to a fragment and adding that fragment to the DOM at the end var frag = document.createDocumentFragment(); $.each(myArray, function(i, item) { var newListItem = document.createElement("li"), itemText = document.createTextNode(item); newListItem.appendChild(itemText); frag.appendChild(newListItem); }); $("#content")[0].appendChild(frag);
String Concatenation
// Appending to a string and generating an HTML tree out of the string and appending it to the DOM var myHtml = ""; $.each(myArray, function(i, item) { myHtml += "<li>" + item + "</li>"; }); $("#content").html(myHtml);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Document Fragment
String Concatenation
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. **What is being tested?** The benchmark is testing the performance of two approaches for appending data to the DOM: 1. **Document Fragment**: This approach uses `document.createDocumentFragment()` to create a fragment that contains multiple elements, which are then appended to the DOM all at once using `appendChild()`. This approach is useful when dealing with large amounts of data and can improve performance by reducing the number of DOM mutations. 2. **String Concatenation**: This approach creates an HTML string by concatenating individual strings using `+` operator or template literals, which are then appended to the DOM using `.html()` method. **Options compared** The two approaches being compared have different pros and cons: * **Document Fragment:** + Pros: - Reduces the number of DOM mutations, which can improve performance. - Can handle large amounts of data without impacting performance. + Cons: - Requires creating a fragment object, which can incur additional overhead. - May not work well with older browsers that don't support `document.createDocumentFragment()`. * **String Concatenation:** + Pros: - Simple and easy to implement. - Works well with most modern browsers. + Cons: - Can be slower due to the overhead of creating an HTML string and parsing it. - May not handle large amounts of data efficiently. **Library usage** The benchmark uses jQuery, a popular JavaScript library for DOM manipulation. The `$.each()` method is used to iterate over the array, and `$().html()` method is used to set the HTML content of the element. The `document.createDocumentFragment()` method is also part of the W3C standard. **Special JS feature or syntax** This benchmark doesn't use any special JavaScript features or syntax that would affect its performance. It's a simple comparison of two approaches, and the focus is on understanding how to optimize DOM manipulation for better performance. **Other alternatives** If you're looking for alternative approaches to improve DOM performance, consider the following: * **Batching**: Instead of appending individual elements to the DOM, batch multiple updates together and apply them in batches. This can reduce the number of DOM mutations. * **Virtual DOM**: Use a virtual DOM library like React or Vue.js, which optimizes DOM manipulation by only updating the difference between the current and previous virtual DOM trees. * **RequestAnimationFrame**: Use `requestAnimationFrame()` to schedule updates to the DOM, which can help improve performance by reducing the time spent in the main thread. Keep in mind that the best approach will depend on your specific use case, browser support requirements, and performance goals.
Related benchmarks:
concat vs push
Array.includes() vs Array.indexOf()
JS arrays..
JS String '+' same v.s. different strings
JS String '+' same v.s. different strings 2
Comments
Confirm delete:
Do you really want to delete benchmark?