Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Avoid reading outside of collection bounds#3769

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
mathiasbynens wants to merge1 commit intojquery:main
base:main
Choose a base branch
Loading
frommathiasbynens:avoid-reading-past-end-of-array

Conversation

@mathiasbynens
Copy link
Contributor

@mathiasbynensmathiasbynens commentedAug 29, 2017
edited
Loading

Summary

Consider the following collection:

constarray=['a','b','c'];

Retrievingarray[0] can be done relatively quickly. However, when the property doesn’t exist on the receiver, JavaScript engines must continue to look up the prototype chain until either the property is found or the chain ends. This is inherently slower thannot doing any prototype chain lookups. Retrieving an out-of-bounds index, e.g.array[3], triggers this scenario, resulting in decreased performance.

This patch changes the way thecleanData loop is written to avoid running into the slow case unnecessarily.

A more in-depth explanation can be found here:https://ripsawridge.github.io/articles/blink-mysterium/

Checklist


Similar patch for Sizzle:jquery/sizzle#407

bmeurer, CYBAI, xu3u4, pirate, screamy, arturkulig, gbabula, calebkleveter, rajikaimal, windyflying, and mallGit reacted with thumbs up emoji
@mention-bot
Copy link

@mathiasbynens, thanks for your PR! By analyzing the history of the files in this pull request, we identified@timmywil,@markelog and@gibson042 to be potential reviewers.

Consider the following collection:    const array = ['a', 'b', 'c'];Retrieving `array[0]` can be done relatively quickly. However, when the property doesn’t exist on the receiver, JavaScript engines must continue to look up the prototype chain until either the property is found or the chain ends. This is inherently slower than *not* doing any prototype chain lookups. Retrieving an out-of-bounds index, e.g. `array[3]`, triggers this scenario, resulting in decreased performance.This patch changes the way some loops are written to avoid running into the slow case unnecessarily.A more in-depth explanation featuring a jQuery-specific example can be found here:https://ripsawridge.github.io/articles/blink-mysterium/
@mathiasbynensmathiasbynensforce-pushed theavoid-reading-past-end-of-array branch from2a1564b to9d8431cCompareAugust 29, 2017 08:12
@markelog
Copy link
Member

Will duplicate sizzle comment here as well :) –

Hey.

This looks nice to me. How this reflects on the byte size though? Maybe you could provide a small jsperf as well?

@mathiasbynens
Copy link
ContributorAuthor

How this reflects on the byte size though?

This change adds 6 bytes after minification + gzip.

Running "compare_size:files" (compare_size) task   raw     gz Sizes271593  80434 dist/jquery.js 87296  30177 dist/jquery.min.js   raw     gz Compared to avoid-reading-past-end-of-array @              9d8431c8329198770212cc8952804bc83d0a8dfd     =      = dist/jquery.js     =      = dist/jquery.min.js   raw     gz Compared to master @ 692f9d4db30c9c6c4f6bc76005cf153586202fa6   +50     +6 dist/jquery.js   +19     +6 dist/jquery.min.js   raw     gz Compared to last run   +50     +6 dist/jquery.js   +19     +6 dist/jquery.min.js

@markelog
Copy link
Member

markelog commentedAug 29, 2017
edited
Loading

This change adds 6 bytes after minification + gzip.

Nice, I think it's worth it. Blog post has interesting insights, but it seems you have to understand Chrome internals to fully appreciate it, that's why I was thinking to check it with our usual approach to the perf pulls.

Thinking a bit further, internally, we were thinking to write loops in same matter across the code base. Maybe this even worth to write a custom eslint rule, so we could put in the jquery preset and all. Since such cases are fairly generalised?

@mathiasbynens
Copy link
ContributorAuthor

Thinking a bit further, internally, we were thinking to write loops in same matter across the code base.

Great idea!

Maybe this even worth to write a custom eslint rule, we can even put in the jquery preset and all. Since such cases are fairly generalised?

👍

cleanData:function(elems){
vardata,elem,type,
special=jQuery.event.special,
length=elems.length,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Do we really need to put the lengths in a variable? I thought most JS engines optimize for this, or at least that the perf difference isn't significant.

refack reacted with thumbs up emoji
@dmethvin
Copy link
Member

Were there only two cases in the code base? I guess the others are in Sizzle and you have that covered with a separate PR. Like@markelog I would be interested in a jsperf to show the difference this makes.

@dmethvin
Copy link
Member

@mathiasbynens did you want to put together a jsperf for this? Given what thecleanData loop does I am not sure this will realy make a difference.

@timmywiltimmywil added this to the3.4.0 milestoneSep 3, 2018
@mgol
Copy link
Member

mgol commentedSep 5, 2018

Key@mathiasbynens, are you still interested in finishing this, including addressing@dmethvin's comments?

@mgolmgol modified the milestones:3.4.0,4.0.0Apr 8, 2019
Base automatically changed frommaster tomainFebruary 1, 2021 22:02
@mgol
Copy link
Member

Closing & re-opening the PR to trigger the EasyCLA check...

@mgolmgol closed thisSep 17, 2021
@mgolmgol reopened thisSep 17, 2021
@linux-foundation-easycla

CLA Not Signed

@timmywiltimmywil modified the milestones:4.0.0,4.1.0Feb 13, 2023
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@dmethvindmethvindmethvin left review comments

At least 1 approving review is required to merge this pull request.

Assignees

No one assigned

Labels

None yet

Milestone

4.1.0

Development

Successfully merging this pull request may close these issues.

6 participants

@mathiasbynens@mention-bot@markelog@dmethvin@mgol@timmywil

[8]ページ先頭

©2009-2025 Movatter.jp