<ul id="update-list">
<li>A</li>
<li>B</li>
<li>C</li>
<li>D</li>
<li>E</li>
</ul>
<ul id="rerender-list">
<li>A</li>
<li>B</li>
<li>C</li>
<li>D</li>
<li>E</li>
</ul>
[].slice
.call(document.querySelectorAll("#update-list li"))
.forEach( (li, idx) => {
li.textContent += " " + idx;
});
const fragment = document.createDocumentFragment();
[].slice
.call(document.querySelectorAll("#rerender-list li"))
.forEach((li, idx) => {
const newLi = document.createElement("li");
newLi.textContent = li.textContent + " " + idx;
fragment.appendChild( newLi );
});
const list = document.querySelector("#rerender-list");
list.innerHTML = "";
list.appendChild(fragment);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
update | |
rerender |
Test name | Executions per second |
---|---|
update | 3948.7 Ops/sec |
rerender | 4366.0 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks on MeasureThat.net.
What is tested?
The provided JSON benchmark definition compares two approaches: updating individual elements and rerendering the entire list.
In the "update" test case, an array of li
elements is sliced from the document query selector #update-list li
. The forEach
method is then used to iterate over each element, incrementing its text content with a counter. This approach updates individual elements without re-rendering the entire list.
In contrast, the "rerender" test case creates a new document fragment and appends it to the original #rerender-list
element. The list's inner HTML is first cleared, and then the updated fragment is appended to it, effectively rerendering the list. This approach rerenders the entire list instead of updating individual elements.
Options compared
The two approaches differ in their impact on performance:
Pros and Cons
Pros:
Cons:
Pros:
Cons:
Library and syntax
The provided benchmark definition uses the following libraries and syntax:
document.querySelectorAll
: A method of the Document object that returns a NodeList containing all elements that match the specified CSS selectors.forEach
: A method of the Array prototype that executes a callback function for each element in an array.Other considerations
When working with lists in JavaScript, consider the following additional factors:
Alternatives
If you're looking for alternatives to MeasureThat.net, consider: