-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Enhancement: no-misused-promises small performance improvement, avoiding unnecessary call to getContextualType #6186
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
What a lovely issue! Thanks so much for the great investigation & deep dive @scottarver - this sounds like a great improvement to the rule. If there's no need to call I'll note, though, that oftentimes it's the first |
I use this rule on quite a large codebase and the rule is taking about 40x time than the average rule. I don't know if the fix suggested will bring the rule down to the level of the average rule, but it sure seems that this rule has a performance issue. |
In the case of my React project, here's the performance report:
I'm analyzing only one file:
Waiting over 25 seconds for a single rule isn't worth having it enabled. |
Try disabling the rule, and see how the time on the others is affected. |
Unfortunately, after disabling it, |
When I remove the rule it cuts 10s(out of 12s it took before) from the entire eslint run time. After using the updated code it took 13s (1s+). I guess it's just that there is a variance to the result and not that the change actually hurts the performance. |
Certain type-aware rules are going to be more expensive than others.
Which all up means the rule is checking a huge percentage of a codebase. More checks means more types which means which means it'll be slower than other rules which are hyper-focused (like Sadly performance hit is just a fact of life though. It's up to you to decide if the extra safety is worth the time it takes. For some it is, for others it isn't. We recommend you use it because a little extra time on your lint saves a lot of potentially painful debugging time later - but we definitely understand those that can't or don't want it! OFC are very open to (and encourage) people helping out here and looking for ways we can improve the performance of rules! |
I made this to try to help it go a bit faster it looks like checking the context is more expensive than checking if something is returning a promise. |
Before You File a Bug Report Please Confirm You Have Done The Following...
Issue Description
I profiled my eslint to see what it is slow, and it lead me to
@typescript-eslint/no-misused-promises
, and the culprate inside isgetContextualType
it appears that
returnsThenable
can be called first beforegetContextualType
forcheckJSXAttribute
profiling a smaller component:

https://github.com/typescript-eslint/typescript-eslint/blob/v5.46.0/packages/eslint-plugin/src/rules/no-misused-promises.ts#L378-L387
It looks like
getContextualType
is more expensive thanreturnsThenable
Inside I saw that there was potential for an optimization to not call
checker.getContextualType(value)
every time. I made this change locally and my lints on my big project went from ~130 seconds to ~125There appears to be some other optimizations that can be done to calls to
getContextualType
but I only looked at 1 place, and I'm not sure of the technicals ofgetContextualType
andreturnsThenable
yeton my large private repo, I got 4311 short circuits and 15 calls to
getContextualType
. so it saved4311
calls togetContextualType
.I am willing to make the PR
Reproduction Repository Link
N/A
Repro Steps
yarn install
yarn lint
NODE_OPTIONS="--inspect-brk"
prefixing the eslint commandVersions
@typescript-eslint/eslint-plugin
5.45.1
@typescript-eslint/parser
5.45.1
TypeScript
4.9.3
ESLint
8.29.0
node
v16.17.0
The text was updated successfully, but these errors were encountered: