Skip to content

Enhancement: no-misused-promises small performance improvement, avoiding unnecessary call to getContextualType #6186

Closed
@scottarver

Description

@scottarver

Before You File a Bug Report Please Confirm You Have Done The Following...

  • I have tried restarting my IDE and the issue persists.
  • I have updated to the latest version of the packages.
  • I have searched for related issues and found none that matched my issue.
  • I have read the FAQ and my problem is not listed.

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 is getContextualType
it appears that returnsThenable can be called first before getContextualType for checkJSXAttribute

profiling a smaller component:
image

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 than returnsThenable

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 ~125

There 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 of getContextualType and returnsThenable yet

on my large private repo, I got 4311 short circuits and 15 calls to getContextualType. so it saved 4311 calls to getContextualType.

I am willing to make the PR

diff --git a/packages/eslint-plugin/src/rules/no-misused-promises.ts b/packages/eslint-plugin/src/rules/no-misused-promises.ts
index b6914ae2..0e85656e 100644
--- a/packages/eslint-plugin/src/rules/no-misused-promises.ts
+++ b/packages/eslint-plugin/src/rules/no-misused-promises.ts
@@ -375,11 +375,16 @@ export default util.createRule<Options, MessageId>({
       ) {
         return;
       }
+      if(!returnsThenable(checker, value.expression)){
+        return;
+      }
       const contextualType = checker.getContextualType(value);
       if (
         contextualType !== undefined &&
-        isVoidReturningFunctionType(checker, value, contextualType) &&
-        returnsThenable(checker, value.expression)
+        isVoidReturningFunctionType(checker, value, contextualType)
       ) {
         context.report({
           messageId: 'voidReturnAttribute',

Reproduction Repository Link

N/A

Repro Steps

  1. clone the repo
  2. yarn install
  3. yarn lint
  4. run NODE_OPTIONS="--inspect-brk" prefixing the eslint command
  5. observe the slowest inside the chrome profiler

Versions

package version
@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

Metadata

Metadata

Assignees

No one assigned

    Labels

    accepting prsGo ahead, send a pull request that resolves this issueenhancementNew feature or requestgood first issueGood for newcomersperformanceIssues regarding performance

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions