Skip to content

Instantly share code, notes, and snippets.

@duzun
Last active August 29, 2015 14:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save duzun/237d728801a49e08f0cb to your computer and use it in GitHub Desktop.
Save duzun/237d728801a49e08f0cb to your computer and use it in GitHub Desktop.
4-10 times faster jQuery.fn.each() replacement
/**
* 4-10 times faster .each replacement
* use it carefully, as it overrides jQuery context of element on each iteration
*
* function clb(DOM, idx, $DOM, DOM) {};
* $DOM == $(DOM), is the same object throughout iteration
*
*/
;(function (global) {
var $ = global.jQuery || global.Zepto;
$.fn.$each = function (clb) {
var self = this
, l = self.length
, i = -1
, j = l > 1 ? $([0]) : self
, d
;
while (
++i < l
&& (j[0] = d = self[i])
&& (!i || (j.context = d)) // .context is deprecated in jQuery v1.10
// clb.call("this"=DOM, i=index, j=jQuery object, d=DOM)
&& clb.call(d, i, j, d) !== false
);
return self
};
})(this);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment