2

For traversing an array-like object, is better use -- for performance -- Array.from( ).forEach() or for loop?

Example of an array-like object:

let childeNodes = document.querySelector('#someid').childNodes;

Iteration with Array.from().forEach():

Array.from(childNodes).forEach(node => {
  // do something with node
})

Or iteration with for:

for (let i = 0; i < childNodes.length; i++) {
  // do something with childNodes[i]
}
3
  • create a jsperf and find out Commented Dec 14, 2017 at 21:52
  • for loop should have a bit better performance, but it doesn't mean you should always use for loop. If you wanna break the loop or make the use of index, then yes. Commented Dec 14, 2017 at 21:53
  • Array.from and a for loop are not analogous. from is only called once. forEach is being called for each item. I would update your question to clarify this point. Commented Dec 14, 2017 at 22:01

3 Answers 3

5

Note that there are two more interesting variants:

  1. The Array.from function accepts a second argument which is a callback called for each element. We can expect that to work faster than the .forEach method appended to it, as no intermediate array has to be created

  2. The ES6 for..of loop

But it is a known fact that the old-fashioned for loop beats all alternatives. Here is a little snippet that makes the measurements:

const childNodes = document.querySelector('#someid').childNodes;
let i, start, duration, times = 500000;

k = times;
start = performance.now();
while (k>0) {
    Array.from(childNodes).forEach(node => 
        k -= node ? 1 : 0
    );
}
duration = performance.now() - start;
console.log('Array.from with forEach: ', duration.toFixed(2));

k = times;
start = performance.now();
while (k>0) {
    Array.from(childNodes, node => 
        k -= node ? 1 : 0
    );
}
duration = performance.now() - start;
console.log('Array.from callback: ', duration.toFixed(2));

k = times;
start = performance.now();
while (k>0) {
    for (let i = 0; i < childNodes.length; i++) {
        k -= childNodes[i] ? 1 : 0;
    }
}
duration = performance.now() - start;
console.log('Oldfashioned for-loop: ', duration.toFixed(2));
    
k = times;
start = performance.now();
while (k>0) {
    for (const node of childNodes) {
        k -= node ? 1 : 0;
    }
}
duration = performance.now() - start;
console.log('for-of loop: ', duration.toFixed(2));
<ul id="someid">
    <li>test1</li>
    <li>test2</li>
    <li>test3</li>
    <li>test4</li>
    <li>test5</li>
</ul>

Sign up to request clarification or add additional context in comments.

Comments

0

Your question is phrased wrong.

Instead of: Array.from vs for(..) It should be: Array.forEach vs for(...)

And for that question you can find very good answers in SO or a simple google search.

tl;dr;

for loop is faster. forEach is slower, but better fits functional programming paradigms.

1 Comment

Convert an array-like object with Array.from adds some overhead?
0

According to this jsperf a for loop is faster than forEach, but a for...in is slowest.

As mentioned though, the Array.from is irrelevant as it's only called once.

Also worth mentioning is that in a real world use case of your example, the slowest operation is likely to be document.querySelector. According to this jsperf using getElementById is much faster.

A larger takeaway here is to understand use cases and maintainability before worrying about performance; and when you do worry about performance, think about it more holistically.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.