Fastest way to convert JavaScript NodeList to Array? Fastest way to convert JavaScript NodeList to Array? arrays arrays

Fastest way to convert JavaScript NodeList to Array?


With ES6, we now have a simple way to create an Array from a NodeList: the Array.from() function.

// nl is a NodeListlet myArray = Array.from(nl)


The second one tends to be faster in some browsers, but the main point is that you have to use it because the first one is just not cross-browser. Even though The Times They Are a-Changin'

@kangax (IE 9 preview)

Array.prototype.slice can now convert certain host objects (e.g. NodeList’s) to arrays — something that majority of modern browsers have been able to do for quite a while.

Example:

Array.prototype.slice.call(document.childNodes);


Here's a new cool way to do it using the ES6 spread operator:

let arr = [...nl];