Created
December 20, 2015 17:35
-
-
Save rolfen/b3e0c37f3752baebd106 to your computer and use it in GitHub Desktop.
Most performant way to cast nodelist to array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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