I've stumbled across jQuery.fly() - flyweight pattern performance benchmark and after looking at the testing code and the plugin code itself (also see below), I can't work out what use is it? I've searched the internet and cannot find any useful information about the plugin itself.
Is it a more efficient way of looping/iterating over an array rather than using $(this)
in .each
?
Iterate using jQuery object
a.each(function() { $(this); });
Iterate using jQuery.fly()
a.each(function() { $.fly(this); });
- almost 2x faster in Firefox 4.0.1
- almost 3x faster in Chrome 12
.fly
(function($) {
var fly = $(),
push = Array.prototype.push;
$.fly = function(elem) {
var len = fly.length,
i;
if ($.isArray(elem)) {
fly.length = 0;
i = push.apply(fly, elem);
} else {
if (elem instanceof $) {
return elem;
}
if (typeof elem == "string") {
throw "use jQuery()";
}
fly[0] = elem;
fly.length = i = 1;
}
// remove orphaned references
while (i<len) {
delete fly[i++];
}
return fly;
};
})(jQuery);