Adding object support to $.map #238

wants to merge 6 commits into from

2 participants


Opened a new ticket:

Old tickets that mention this: and

Since it was 3 years ago, it didn't seem like it's a huge priority. I wanted to make the addition to handle objects without sacrificing the raw speed of $.map for arrays. Underscore.js already implements this:

Here are some benchmarks:

The pull request contains test: $.new_map_forin_2.

Some more tests here:

jQuery Foundation member

Instead of the length === undefined stored as isObj, why not use jQuery.type( elems).. Because I might have an object with a length property. Also, the indentation of the if block is to far


Cool. Yeah I was thinking of using isPlainObject, but that's a little slower. I checked jQuery.each and saw it was using
isObj = length === undefined; so I figured I would just use the same. Should it not be changed in jQuery.each as well? I figured it hadn't been changed because it's less performant to do an actual object check using isPlainObject

I tried changing it to jQuery.type( elems ) == 'object' but it seemed to crash the unit tests :p

I will patch it using isPlainObject


Hmm.. isPlainObject is less performant... upon more thinkerizing...
I wonder if having the "length === undefined" is sufficient for speed purposes... seems like an edge case where someone would pass an object with a key of "length". Having a "length" key currently does not work with the current $.each implementation:

Looking for feedback on this :)

jQuery Foundation member

Code solution aside, the if block is still indented too far.


re-opening pull request under a proper branch


This pull request can be found here: #252

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