Ticket #4262 (closed enhancement: fixed)
eq() performance improvement and negative indices
| Reported by: | ricardobeat | Owned by: | gibson042 |
|---|---|---|---|
| Priority: | minor | Milestone: | |
| Component: | core | Version: | 1.3.2 |
| Keywords: | eq, performance | Cc: | ricardobeat |
| Blocking: | Blocked by: |
Description
Currently $(...).eq(x) is much slower than $( $(..)[x] ). This patch improves performance by pushing the single element into a new object via pushStack.
Attachments
Change History
comment:1 Changed 4 years ago by ricardobeat
Notice that the 'selector' string will now read ".eq(1)" instead of ".slice(1,2)", hope that's not a problem.
Changed 4 years ago by ricardobeat
-
attachment
jquery.eq.patch
added
patch with support for negative indexes
comment:2 Changed 4 years ago by ricardobeat
Added support for negative indexes to comply with the changes in get().
http://groups.google.com/group/jquery-dev/browse_thread/thread/432e256106530123?hl=en
comment:3 Changed 4 years ago by ricardobeat
Oh, could someone change the ticket description to include this enhancement? Or should I have put this in a separate ticket?
comment:4 Changed 4 years ago by dmethvin
- Summary changed from eq() performance improvement to eq() performance improvement and negative indices
Dup #4731 has an alternate patch for negative indices.
comment:7 Changed 7 months ago by Christian Meixner <christian.meixner@…>
.eq(i) still is much slower than $($(..)[i])
see jsperf: http://jsperf.com/jquery-eq
Why not just return $($(..)[i]) in .eq()?
return $(collection[i < 0 ? collection.length - i: i ]);
The current implementation of .eq() can not be recommended for loops.
comment:8 Changed 7 months ago by ajpiano
- Status changed from closed to reopened
- Resolution fixed deleted
- Milestone 1.4 deleted
That jsPerf shows jQuery 1.6.1 - I made another that shows 1.6.1 and 1.8.2 side by side, where the fundamental problem persists. http://jsperf.com/jq-eq-index-vs-jq-jqobj-index
We can't return jQuery( jQuery(...)[i] ) because it breaks the stack, behaviour of .end(), etc. but I do agree that it would be good to get to the bottom of why it's so dramatically slower here, though I suspect that it can be chalked up to the fact that the various operations required to retain the expected API behaviour bring with them some inherent overhead
comment:9 Changed 7 months ago by gibson042
- Owner changed from flesler to gibson042
- Status changed from reopened to assigned
comment:10 Changed 7 months ago by Richard Gibson
- Status changed from assigned to closed
- Resolution set to fixed
Fix #4262: faster .eq(), closes gh-1000.
Changeset: b5084b4bf29a2e517b90d5e7d82ed17c94a71d94
Please follow the bug reporting guidlines and use jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

patch