Core: Refactor loops to avoid going behind array bounds#5839
Open
mgol wants to merge 1 commit into
Open
Conversation
Member
Author
|
Current size diffs: |
mgol
commented
May 11, 2026
| // results (starting at index 1 because index 0 is always kept), then | ||
| // splice away the tail of duplicates. | ||
| for ( ; i < results.length; i++ ) { | ||
| for ( i = 1; i < results.length; i++ ) { |
Member
Author
There was a problem hiding this comment.
@gibson042 changes in this file resulted in the following size diffs compared to if I didn't touch the file:
last run
raw gz br Filename
+1 -7 +17 dist/jquery.min.js
+1 -6 -18 dist/jquery.slim.min.js
+1 -6 +35 dist-module/jquery.module.min.js
+1 -6 +31 dist-module/jquery.slim.module.min.js
Member
Author
There was a problem hiding this comment.
It's possible it'd go the other way round if we avoided initializers in loops and moved them to declarations, but it's not always possible, as sometimes the same variable is used in multiple loops.
c15d743 to
b3b1d00
Compare
Reaching behind array bounds - which is what effectively happens if you check for `null`/`undefined` value instead of doing a `length` check - can trigger deopts in various JS engines. Switch to regular `length`-based loops. In the past, this was impractical as IE <9 used to clobber the `length` property if an element with the `name="length"` attribute was present, but this is no longer a concern.
b3b1d00 to
4238cfd
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Reaching behind array bounds - which is what effectively happens if you check for
null/undefinedvalue instead of doing alengthcheck - can trigger deopts in various JS engines. Switch to regularlength-based loops.In the past, this was impractical as IE <9 used to clobber the
lengthproperty if an element with thename="length"attribute was present, but this is no longer a concern.Checklist
New tests have been added to show the fix or feature worksIf needed, a docs issue/PR was created at https://github.com/jquery/api.jquery.com