Last active
August 29, 2015 14:09
-
-
Save duzun/237d728801a49e08f0cb to your computer and use it in GitHub Desktop.
4-10 times faster jQuery.fn.each() replacement
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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