Ticket #9525 (closed enhancement: duplicate)
performance: a really fast single-element $() wrapper?
|Reported by:||shadedecho||Owned by:|
A common (and major) performance anti-pattern in jquery is the oft-seen $(elem) single-element collection wrapping, which invokes the performance-heavy $() constructor for the special case where all someone wants is the special jquery decorations on top of a single element (don't need collection/stack management).
Inside jquery functions like .each(), it's extremely common to have the pattern of $(this) wrapping, because someone wants to call the special jquery operations/decorations on the object instead of doing bare DOM element manip. It's quite rare in that pattern for someone to be setting up a jquery collection which they will add elements to, etc. They really just need (usually) a single-element collection, and a faster wrapper would be very nice for that purpose.
As an example:
Ext.fly() is apparently similar (disclaimer, I have no Ext experience to speak of). The idea is to have a quick and fast $()-like constructor for creating a jquery object wrapper around only a single-element. In other words, no need for all the stack manipulation stuff (ostensibly seems is what slows down $() construction).
In my jsperf test, I created $_(). I think it's possible that $() might be special-cased in some way, but I honestly think that such a constructor should be a special name, because the single-element collection is creates is special, and has caveats (like no stack manipulation, etc).
Also, *my* $_() is quite flawed, as doing this from the outside is (afaict) impossible without the caveat as noted. But it seems like internally jquery could easily and quickly create the object wrapper, and simply assign the first and only element via the elem.context = elem = obj approach.
- Status changed from new to closed
- Resolution set to duplicate
- Priority changed from undecided to low
- Component changed from unfiled to core