JavaScript: Convert NodeList to Array

NodeLists are very similar to Array collections of elements, often referred to as “array-like”, but don’t feature many of the methods provided by the true Array, like forEach, map, filter, etc. JavaScript does,

The result of converting NodeList to Array below is a true Array object containing all of the nodes returned

1. Array.prototype.slice.call

2. Push

3. Single for

4. Set length first

5. Set length first 2

6. Terse While new Array

7. Push.apply

8. Array.unshift

9. Bitwise operation

After performance test, method 6 “Terse While new Array” are the fastest method

Leave a Reply

Your email address will not be published. Required fields are marked *

CAPTCHA