<p id="p">hello</p>
'use strict';
let p = document.getElementById('p')
for(let i = 1000; i--;) p.appendChild(document.createElement('p'))
function myFunc(node) {
node.remove()
}
[].forEach.call(p.getElementsByTagName('*'), myFunc)
for(let node of p.getElementsByTagName('*'))node.remove()
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
[].forEach.call | |
for...of |
Test name | Executions per second |
---|---|
[].forEach.call | 3282093.8 Ops/sec |
for...of | 3134956.8 Ops/sec |
The benchmark you're examining focuses on performance comparisons for iterating over a collection of DOM nodes in a web page. Specifically, it tests how two different JavaScript approaches—[].forEach.call()
and for...of
—perform when applied to an HTML collection obtained from a DOM element.
The benchmark involves a preparation code that builds a document structure consisting of a <p>
element with multiple child <p>
elements. The preparation code creates 1000 child paragraphs and appends them to the initial paragraph:
'use strict';
let p = document.getElementById('p');
for(let i = 1000; i--;) p.appendChild(document.createElement('p'));
function myFunc(node) {
node.remove();
}
The myFunc
function removes a node from the DOM, which will be invoked for each child element during the benchmark tests.
Test Case 1: [].forEach.call(p.getElementsByTagName('*'), myFunc)
forEach()
method of an array, leveraging the .call
function to apply it to NodeLists returned by getElementsByTagName('*')
. Essentially, it converts the NodeList to an array-like structure, iterates over it, and applies the myFunc
to remove nodes.Test Case 2: for(let node of p.getElementsByTagName('*')) node.remove()
for...of
loop, which directly iterates over the collection returned by getElementsByTagName()
. It does not require conversion to an array.for...of
loop is usually more efficient for iterating over iterable objects, as it avoids the overhead of creating an intermediate array. It’s also easier to read, especially for developers comfortable with modern JavaScript syntax.for...of
might not be as familiar to developers coming from older JavaScript backgrounds, although it is part of the ES6 specification and increasingly popular in contemporary JavaScript codebases.The performance metrics show:
[].forEach.call
: 3,282,093.75 executions per second.for...of
: 3,134,956.75 executions per second.From the results, the forEach.call
approach performs slightly better in this specific environment and benchmark, but both methods yield performance results in a comparable range.
for...of
.forEach
easier, while experienced developers might prefer the for...of
loop).Other alternatives for iterating over a NodeList could include:
for
Loop: for (var i = 0; i < nodeList.length; i++) { nodeList[i].remove(); }
provides the most control and can be the most performant in certain cases, though it's more verbose and less elegant.Array.from()
: If it's acceptable to convert NodeList to an array ahead of time, Array.from(p.getElementsByTagName('*')).forEach(myFunc);
can also be used. This might incur similar overhead to forEach.call
.In summary, both iterating techniques provide efficient ways to remove DOM nodes, with slight performance variances. The best choice largely depends on the specific context of the application, team standards, and target browser support.