When traversing an array-like object, which method is more efficient for performance: using Array.from( ).forEach()
or a traditional for
loop?
An example of an array-like object would be:
let elements = document.querySelector('#someid').children;
Here is an example of iteration using Array.from().forEach()
:
Array.from(elements).forEach(element => {
// do something with the element
})
Alternatively, here is an example of iteration using for
:
for (let i = 0; i < elements.length; i++) {
// do something with elements[i]
}