<p id="p">hello</p>
let p = document.getElementById('p')
for(let i = 300; i--;) p.appendChild(document.createElement('p'))
function myFunc(node) {
node.remove()
}
[].forEach.call(p.querySelectorAll('*'), myFunc)
for(let node of p.querySelectorAll('*'))node.remove()
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
[].forEach.call | |
for...of |
Test name | Executions per second |
---|---|
[].forEach.call | 1980651.5 Ops/sec |
for...of | 1912658.8 Ops/sec |
The provided benchmark is designed to assess the performance of two different JavaScript approaches for iterating over and manipulating an array-like collection of DOM elements. The core functionality being tested is the removal of child elements from a parent <p>
element with various methods for iteration.
Test Name: looping through arrayLike
Script Preparation Code:
<p>
child elements inside a parent <p id="p">
.myFunc(node)
that removes a given DOM node.HTML Preparation Code:
The benchmark consists of two individual test cases:
Test Case 1: [].forEach.call(p.querySelectorAll('*'), myFunc)
Array.prototype.forEach
function, leveraging call
to treat the NodeList returned by querySelectorAll('*')
as an array. It applies myFunc
to each element in the list, effectively removing each from the DOM.forEach
is widely used for array manipulation.Test Case 2: for(let node of p.querySelectorAll('*')) node.remove()
for...of
loop, which allows direct iteration over iterable objects. Here, it's used to iterate over each node in the NodeList obtained from querySelectorAll('*')
and calls the native remove()
method on each node.for...of
directly iterates through the collection without additional function calls or context changes.[].forEach.call
: 1,980,651.5 executions per secondfor...of
: 1,912,658.75 executions per second[].forEach.call
method appears slightly faster than for...of
, although the differences in execution speed may be minimal depending on the context of the execution environment and size of the NodeList.Library Usage: This benchmark does not include any third-party libraries; it solely utilizes native JavaScript functionality to manipulate DOM elements.
JavaScript Features: The test does not employ any special JavaScript features that would require deep familiarity with the language. It uses standard constructs available in ES5 and ES6, which are fundamental to JavaScript programming.
Traditional For Loop:
for
loop to iterate over the NodeList by index. This might provide even better performance in certain scenarios because it avoids the overhead of function calls or the need for additional prototypes.forEach
on NodeList:
NodeList
to be used with forEach
directly without the need for call
, allowing for a more natural iteration structure. For example, p.querySelectorAll('*').forEach(myFunc)
.Other Enumeration Methods:
map()
, filter()
, or reduce()
functions can also be used for different manipulation tasks, but might not be directly applicable for simply removing nodes as intended in this benchmark.In conclusion, the choice of iteration method can significantly impact performance, especially in tasks involving DOM manipulation. Developers should consider the specific context and performance implications of each approach for optimal results in their applications.