Skip to content

Instantly share code, notes, and snippets.

@cowboy
Forked from padolsey/gist:500145
Created August 1, 2010 13:32
Show Gist options
  • Select an option

  • Save cowboy/503362 to your computer and use it in GitHub Desktop.

Select an option

Save cowboy/503362 to your computer and use it in GitHub Desktop.
Like .quickEach, but subtly different
// Plugin released:
// http://benalman.com/projects/jquery-misc-plugins/#each2
@padolsey

padolsey commented Aug 1, 2010

Copy link
Copy Markdown

Nice! Thanks for having a look Ben!

The try-catch is to make sure that jq[0] is cleared (even if an exception is thrown within the callback) -- otherwise memory leaks could potentially occur -- it's an edge case, but still...

@padolsey

padolsey commented Aug 1, 2010

Copy link
Copy Markdown

I like the backwards-compatible each ... that may be a better option than adding a new quickEach method.

@cowboy

cowboy commented Aug 1, 2010

Copy link
Copy Markdown
Author

James, check out the new, third, approach.. it might be a good compromise!

@cowboy

cowboy commented Aug 1, 2010

Copy link
Copy Markdown
Author

Note that there is some extra-args-passing that the current jQuery.fn.each does that would have to be added in, but that's just an array concat / apply.

@malsup

malsup commented Aug 1, 2010

Copy link
Copy Markdown

$([]).length == $().length == 0

@padolsey

padolsey commented Aug 2, 2010

Copy link
Copy Markdown

Can this be added to the core? I think it would be worth it!

Also, I think it'd be best to add the try-catch -- an exception thrown in a callback several times could cause a memory leak -- I might be completely wrong about this, but the fact that jq[0] won't be cleared presents a potential issue.

@padolsey

padolsey commented Aug 2, 2010

Copy link
Copy Markdown

Plus I think the backwards-compatible suggestion is definitely the way to go!

@cowboy

cowboy commented Aug 2, 2010

Copy link
Copy Markdown
Author

Mike, any time I post/release a plugin, if I can make it backwards-compatible with older jQuery versions easily I will.. Since $([]) is a backwards-compatible way of "selecting" an empty set, I'll use that. Of course, for older jQueries, selecting document won't really consuming a whole lot of memory, but still.. that's how I roll!

@cowboy

cowboy commented Aug 2, 2010

Copy link
Copy Markdown
Author

James, I was thinking of releasing this as a standalone "jQuery fast .each" plugin, with code pretty much just as it is in this gist, and with the callback signature fn.call( dom_elem, i, jq_object ). Would you object to that? (I don't want to step on your toes here)

I also definitely think that this should be considered for inclusion in core, with Resig making whatever decision regarding its signature/context if necessary, should it ever get to that point. I personally feel that changing jQuery's .each signature from fn.call( dom_elem, i, dom_elem ) to fn.call( dom_elem, i, jq_object ) would be acceptable, but that's obviously not my call.

@cowboy

cowboy commented Aug 2, 2010

Copy link
Copy Markdown
Author

Ok, major correction.. this is going to be WAY slower than the core .each in scenarios where the user doesn't need a jQuery object inside the callback... so it should definitely be a separate method. See http://jsperf.com/jquery-each-vs-quickeach/2

@cowboy

cowboy commented Aug 3, 2010

Copy link
Copy Markdown
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment