Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Improve performance of get() for negative indices. Fixes #5476. #102

Merged
1 commit merged into from over 3 years ago

1 participant

Anton M.
Anton M.

Check the bug ticket for discussion and a performance test.

This commit also fixes an inconsistency in the return values of get() e.g.

bar


$("#foo").get(0) //yields the element
$("#foo").get(1) //yields undefined
$("#foo").get(-1); //yields the element
$("#foo").get(-2); //still yields the element #foo instead of undefined
//new version return undefined for the last use case
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Nov 19, 2010
Anton M. jitter Improve performance of get() for negative indices. Fixes #5476. 5c0b5d2
This page is out of date. Refresh to see the latest.

Showing 2 changed files with 6 additions and 6 deletions. Show diff stats Hide diff stats

  1. +1 1  src/core.js
  2. +5 5 test/unit/core.js
2  src/core.js
@@ -215,7 +215,7 @@ jQuery.fn = jQuery.prototype = {
215 215 this.toArray() :
216 216
217 217 // Return just the object
218   - ( num < 0 ? this.slice(num)[ 0 ] : this[ num ] );
  218 + ( num < 0 ? this[ this.length + num ] : this[ num ] );
219 219 },
220 220
221 221 // Take an array of elements and push it onto the stack
10 test/unit/core.js
@@ -547,15 +547,15 @@ test("toArray()", function() {
547 547 })
548 548
549 549 test("get(Number)", function() {
550   - expect(1);
  550 + expect(2);
551 551 equals( jQuery("p").get(0), document.getElementById("firstp"), "Get A Single Element" );
  552 + strictEqual( jQuery("#firstp").get(1), undefined, "Try get with index larger elements count" );
552 553 });
553 554
554 555 test("get(-Number)",function() {
555   - expect(1);
556   - equals( jQuery("p").get(-1),
557   - document.getElementById("first"),
558   - "Get a single element with negative index" )
  556 + expect(2);
  557 + equals( jQuery("p").get(-1), document.getElementById("first"), "Get a single element with negative index" );
  558 + strictEqual( jQuery("#firstp").get(-2), undefined, "Try get with index negative index larger then elements count" );
559 559 })
560 560
561 561 test("each(Function)", function() {

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.