Posts

Showing posts with the label convertion

Array.prototype.slice VS Arrayfication

One of the most common operations performed on daily basis directly or indirectly via frameworks and libraries is Array.prototype.slice calls over non Array elements such HTMLCollection , NodeList , and Arguments . Why We Perform Such Operation The Function.prototype.apply works only with object created through the [[Class]] Array or Arguments. In latter case we may like to avoid ES3 arguments and named arguments mess when dealing with indexes. Finally, in most of the case we would like to perform Array operations over ArrayLike objects to filer, map, splice, change, modify, etc etc ... The Slice Cost Let's perform over an ArrayLike object of length 2000, 2000 slice calls (change the length if you have such powerful machine): // create the ArrayLike object for(var arguments = {length:2000}, i = 0; i arguments[i] = i; ; // define the bench function function testSlice(arguments) { var t = new Date; for (var slice = Array.prototype.slice, i = 0, length = arguments.lengt...