69

I have a config with around 100 rules, and running eslint on my project with all these rules takes around 10 seconds. I'd like to identify the slowest rules and eliminate some of them. How do I do this? Is there any profiler tool for eslint?

mik01aj
  • 11,928
  • 15
  • 76
  • 119

4 Answers4

144

eslint shows the spent times of rules if the environment variable TIMING is set. For example:

$ TIMING=1 eslint lib
Rule                         | Time (ms) | Relative
:----------------------------|----------:|--------:
valid-jsdoc                  |   203.798 |     6.7%
camelcase                    |   142.146 |     4.6%
no-unmodified-loop-condition |   136.811 |     4.5%
indent                       |   127.138 |     4.2%
no-undefined                 |   124.525 |     4.1%
keyword-spacing              |    85.397 |     2.8%
space-in-parens              |    76.179 |     2.5%
no-this-before-super         |    72.317 |     2.4%
no-implied-eval              |    69.945 |     2.3%
space-infix-ops              |    57.128 |     1.9%

See also the official docs on Per-rule Performance.

mik01aj
  • 11,928
  • 15
  • 76
  • 119
mysticatea
  • 1,917
  • 1
  • 12
  • 12
  • 1
    Wow, it's that easy! Thank you! – mik01aj Jul 20 '16 at 08:12
  • 21
    Too bad it doesn't show the parser timer. I'm using `@typescript-eslint/parser` and it's taking a long time, wanted a benchmark on it. – Ka Mok Feb 06 '19 at 19:05
  • 1
    Indeed, I have the same issue with the `@typescript-eslint/parser`, with a file of 400 lines it takes about 6 seconds, the actual rules are comparatively fast, 600ms being the slowest and also (99.6% of the relative). So at least 5.4 seconds disappearing to parsing. An unfortunate thing with the `--cache` is that it doesn't work with `--stdin` something which tripped me up in trying to optimize this as an editor integration. – terje May 20 '19 at 20:16
35

I found that removing slow rules didn't really help that much, as loading eslint and parsing files takes a while.

It is possible to use the --cache option of eslint (docs) to speed things up substantially.

When using eslint to "lint-as-you-type" in various editors, installing eslint_d allows running eslint as a daemon, and saves the node loading time.

On the project I'm currently working on, combining both eslint_d and --cache brought the linting time from 4+ seconds to 0.17!

Laurent S
  • 4,106
  • 3
  • 26
  • 50
  • 2
    I use vim with the [ALE](https://github.com/dense-analysis/ale) plugin for linting. Setting `let g:ale_javascript_eslint_executable = 'eslint_d --cache'` to use both `eslint_d` and `--cache` speed it up from ~1 second to be almost instant! – Giovanni Benussi Oct 11 '19 at 12:53
  • I use eslint via the Syntastic plugin for VIM. Any info about how to enable `--cache` while running from Syntastic? – nullromo Jun 09 '20 at 20:34
  • I don't use syntastic myself, but the [docs](https://github.com/vim-syntastic/syntastic#faqcheckers) under section 4.5 mention how to pass options to a checker, I'm guessing in your case something like `let g:syntastic_javascript_eslint_args = "--cache"` – Laurent S Jun 10 '20 at 12:23
  • 1
    There's a few options I had to change to get `eslint_d` working correctly with syntastic. `let g:syntastic_javascript_checkers = ['eslint'] let g:syntastic_javascript_eslint_exe = 'eslint_d' let g:syntastic_javascript_eslint_args = "--cache" let g:syntastic_javascript_eslint_exec = '/bin/ls'` Lastly, you need to ensure `eslint_d` is in the PATH when vim is launched. – lukecampbell Jul 10 '20 at 17:18
  • How can we use this with `create-react-app` ? – SeyyedKhandon Aug 01 '21 at 08:19
  • This is the solution to this problem, and I'm pretty sad that pretty much no other threads anywhere mention `eslint_d`. – kevr Jan 04 '23 at 04:42
0

In my case I'm using @typescript-eslint/eslint-plugin (linting with type information) and I'd misconfigured the tsconfig include field. Based on this doc you have to include all your files. So I updated my eslint configuration:

module.exports = {
  overrides: [
    {
      files: ['*.ts'],
      parserOptions: {
-       project: ['tsconfig.json'],
-       createDefaultProgram: true,
+       project: ['tsconfig.json', './projects/*/tsconfig.json'],
+       createDefaultProgram: false,
      },
    }
  ]
}

This can also happen if you're using @angular-eslint/eslint-plugin. Read the performance section of their docs

Vahid
  • 6,639
  • 5
  • 37
  • 61
  • I think the relevant `@typescript-eslint` doc is now here: https://github.com/typescript-eslint/typescript-eslint/blob/main/docs/linting/MONOREPO.md – charles-allen Dec 11 '21 at 06:18
0

I encountered this issue as well, this was because I enabled createDefaultProgram: true, removing it increased my performances significantly !!

Yohan Dahmani
  • 1,670
  • 21
  • 33