My JavaScript book is out! Don't miss the opportunity to upgrade your beginner or average dev skills.

Friday, April 03, 2009

A fast Array slice for every browser

It is an extremely common task and one of the most used prototype in libraries and applications: Array.prototype.slice
The peculiarity of this prototype is to create an Array from an Array like Object such arguments, HTMLCollection, other kind of lists as jQuery results.

Every browser, except Internet Explorer, allows direct calls via native prototype obtaining, obviously, best performances.

In IE, we have different ways to make this task possible, and these ways are mainly classic loops, or the isArray check to know when it is possible to perform the native call or not.

isArray = (function(toString){
return function(obj){
return === "[object Array]";

Even using a closure, above function could slow down performances, specially in those libraries where Array conversions are performed almost everywhere.

As we all know, Internet Explorer behaves weird "sometimes", and one of the weirdest things is that native Objects are not instanceof Object.

In few words, instead of call a function to define if native slice could be applied, all we need to do is to check if the generic object is instanceof Object.
In this way native Arrays, arguments, everything with a length user defined, will be passed via native Array.prototype.slice, while for native objects, the good old loop will do the dirty job.

A better general purpose slice

$slice = (function(slice){
// WebReflection - Mit Style License
try {
// Chrome, FireFox, Opera, Safari, WebKit;
var $slice = slice;
} catch(e) {
// Internet Epxlorer
var $slice = function(begin, end){
// false with native objects/collections
// suitable for Array like Object (e.g arguments, jQuery, etc ...)
if(this instanceof Object)
return, begin || 0, end || this.length);
// ... every other case ...
for(var i = begin || 0, length = end || this.length, len = 0, result = []; i < length; ++i)
result[len++] = this[i];
return result;
return $slice;

Simple, isn't it? And here a test case:
try {

// HTMLCollection [object, ...]

// arguments [3,2,1]

// Array [1,2,3]

} catch(e) {

// should never happen

where if nothing happen, simply means $slice worked without problems.

Side effects? With sandbox variables (iframes) IE will always use the loop, unless we do not bring its native Object constructor into the function (or the function into the sandbox)

3 comments: said...

IMHO:the copy loop is broken, does not work for negative begin and end params

the same code is in your ArrayObject as well, so please fix both

6,0 //zero is bug 5, 6-1 should be result

var ao = document.getElementsByTagName("*");
alert($, 0, -1).length);

var a = [1, 2, 3];
alert($, 0, -1).length); said...

No reply ?

Andrea Giammarchi said...

holidays man, I'll check ASAP and thanks for the hint ;)