Skip to main content

Bug Tracker

Side navigation

#4262 closed enhancement (fixed)

Opened February 26, 2009 09:58PM UTC

Closed October 20, 2012 02:34AM UTC

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:

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 (0.3 KB) - added by ricardobeat February 26, 2009 09:58PM UTC.


  • jquery.eq.patch (0.4 KB) - added by ricardobeat February 28, 2009 07:48PM UTC.

    patch with support for negative indexes

Change History (10)

Changed February 26, 2009 09:59PM UTC by ricardobeat comment:1

Notice that the 'selector' string will now read ".eq(1)" instead of ".slice(1,2)", hope that's not a problem.

Changed February 28, 2009 07:50PM UTC by ricardobeat comment:2

Added support for negative indexes to comply with the changes in get().

Changed February 28, 2009 07:51PM UTC by ricardobeat comment:3

Oh, could someone change the ticket description to include this enhancement? Or should I have put this in a separate ticket?

Changed June 08, 2009 01:02AM UTC by dmethvin comment:4

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

Dup #4731 has an alternate patch for negative indices.

Changed September 14, 2009 06:51PM UTC by flesler comment:5

cc: → ricardobeat
owner: → flesler

Changed November 11, 2009 06:10PM UTC by john comment:6

resolution: → fixed
status: newclosed

Changed October 16, 2012 07:54AM UTC by Christian Meixner <> comment:7

.eq(i) still is much slower than $($(..)[i])

see jsperf:

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.

Changed October 19, 2012 01:33AM UTC by ajpiano comment:8

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.

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

Changed October 19, 2012 09:12PM UTC by gibson042 comment:9

_comment0: \
owner: fleslergibson042
status: reopenedassigned

Changed October 20, 2012 02:34AM UTC by Richard Gibson comment:10

resolution: → fixed
status: assignedclosed

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

Changeset: b5084b4bf29a2e517b90d5e7d82ed17c94a71d94