Skip to content

Instantly share code, notes, and snippets.

@rolfen
Created December 20, 2015 17:35
Show Gist options
  • Save rolfen/b3e0c37f3752baebd106 to your computer and use it in GitHub Desktop.
Save rolfen/b3e0c37f3752baebd106 to your computer and use it in GitHub Desktop.
Most performant way to cast nodelist to array
/**
* Performant nodelist to array cast. Source: http://stackoverflow.com/a/15144269/370786
* @param nl {NodeList} NodeList to convert. Might work with other data types (Array?) but that needs further testing.
*/
function toArray(nl)
{
var arr = [];
if(isNaN(nl.length)) {
throw "Cannot get length";
} else {
for(var i=-1,l=nl.length;++i!==l;arr[i]=nl[i]);
}
return(arr);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment