Bug Tracker

Opened 11 years ago

Closed 7 years ago

#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
Blocked by: Blocking:

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 (2)

eq.patch (336 bytes) - added by ricardobeat 11 years ago.
patch
jquery.eq.patch (374 bytes) - added by ricardobeat 11 years ago.
patch with support for negative indexes

Download all attachments as: .zip

Change History (12)

Changed 11 years ago by ricardobeat

Attachment: eq.patch added

patch

comment:1 Changed 11 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 11 years ago by ricardobeat

Attachment: jquery.eq.patch added

patch with support for negative indexes

comment:2 Changed 11 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 11 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 10 years ago by dmethvin

Summary: eq() performance improvementeq() performance improvement and negative indices

Dup #4731 has an alternate patch for negative indices.

comment:5 Changed 10 years ago by flesler

Cc: ricardobeat added
Owner: set to flesler

comment:6 Changed 10 years ago by john

Resolution: fixed
Status: newclosed

comment:7 Changed 7 years 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 years ago by ajpiano

Milestone: 1.4
Resolution: fixed
Status: closedreopened

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 years ago by gibson042

Owner: changed from flesler to gibson042
Status: reopenedassigned
Last edited 7 years ago by gibson042 (previous) (diff)

comment:10 Changed 7 years ago by Richard Gibson

Resolution: fixed
Status: assignedclosed

Fix #4262: faster .eq(), closes gh-1000.

Changeset: b5084b4bf29a2e517b90d5e7d82ed17c94a71d94

Note: See TracTickets for help on using tickets.