public
Last active — forked from padolsey/gist:500145

  • Download Gist
gistfile1.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
// The `quickEach` method will pass a non-unique jQuery instance
// to the callback meaning that there will be no need to instantiate
// a fresh jQuery instance on each iteration. Most of the slow-down
// inherent in jQuery's native iterator method (`each`) is the constant
// need to have access to jQuery's methods, and so most developers
// see constructing multiple instances as no issue... E.g.
// $(...).each(function(){ $(this)... $(this)... $(this)... });
// A better approach would be `quickEach`.
 
jQuery.fn.quickEach = (function(){
var jq = jQuery([1]);
return function(c) {
 
var i = -1, el, len = this.length;
 
try {
 
while (
++i < len &&
(el = jq[0] = this[i]) &&
c.call(jq, i, el) !== false
);
 
} finally {
delete jq[0];
}
 
return this;
};
}());
 
// E.g.
 
jQuery('div').quickEach(function(i){
// `this` is a jQuery object
this.attr('id', 'd' + i).css('color', 'red'); // etc.
});

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.