Skip to content
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

Prevent optional dependency Chokidar from loading when not watching #1250

Merged
merged 1 commit into from
Oct 17, 2019

Conversation

eklingen
Copy link
Contributor

Summary

Proposed change:

Since Chokidar is an optional dependency, and relatively heavy, it would be best to defer requiring it until the watcher is actually needed. This gives a slight speed increase when using FileSystemResolver or NodeResolver with the watch option set to false, unless the end-user has already loaded Chokidar.

Triggering require() twice when using both loaders does not have a big performance impact because of the module cache. This is an internal change only. I have not written tests, since these would depend upon module.cache[] side-effects to run.

Checklist

I've completed the checklist below to ensure I didn't forget anything. This makes reviewing this PR as easy as possible for the maintainers. And it gets this change released as soon as possible.

@codecov-io
Copy link

codecov-io commented Oct 15, 2019

Codecov Report

Merging #1250 into master will decrease coverage by 0.06%.
The diff coverage is 0%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1250      +/-   ##
==========================================
- Coverage    89.6%   89.53%   -0.07%     
==========================================
  Files          22       22              
  Lines        3020     3020              
==========================================
- Hits         2706     2704       -2     
- Misses        314      316       +2
Impacted Files Coverage Δ
nunjucks/src/node-loaders.js 65.67% <0%> (-2.99%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 1485a44...cefb1cf. Read the comment docs.

@eklingen
Copy link
Contributor Author

As you can see from this trace, a require('nunjucks') call takes (on my system) ~177ms, of which ~160ms is spent on importing chokidar, even when watch is set to false. With this change, nunjucks takes only ~17ms to load. Every little bit helps. ;)

  177 ms            └─ node_modules/nunjucks/index.js (10x)
  174 ms                └─ node_modules/nunjucks/src/environment.js (8x)
    9 ms                    ├─ node_modules/nunjucks/src/compiler.js (6x)
    4 ms                    │   └─ node_modules/nunjucks/src/parser.js (3x)
  162 ms                    └─ node_modules/nunjucks/src/loaders.js
  161 ms                        └─ node_modules/nunjucks/src/node-loaders.js
  160 ms                            └─ node_modules/chokidar/index.js (10x)
  107 ms                                ├─ node_modules/anymatch/index.js (3x)
  106 ms                                │   └─ node_modules/micromatch/index.js (7x)
   80 ms                                │       ├─ node_modules/braces/index.js (7x)
   13 ms                                │       │   ├─ node_modules/to-regex/index.js (4x)
    4 ms                                │       │   │   ├─ node_modules/safe-regex/index.js
    4 ms                                │       │   │   └─ node_modules/to-regex/node_modules/define-property/index.js
    5 ms                                │       │   ├─ node_modules/braces/lib/compilers.js
    5 ms                                │       │   ├─ node_modules/braces/lib/parsers.js (2x)
   56 ms                                │       │   └─ node_modules/braces/lib/braces.js (4x)
   54 ms                                │       │       └─ node_modules/snapdragon/index.js (5x)
   28 ms                                │       │           ├─ node_modules/base/index.js (6x)
   13 ms                                │       │           │   ├─ node_modules/cache-base/index.js (4x)
    8 ms                                │       │           │   │   └─ node_modules/unset-value/index.js
    6 ms                                │       │           │   └─ node_modules/class-utils/index.js (3x)
   14 ms                                │       │           └─ node_modules/snapdragon/lib/compiler.js
   13 ms                                │       │               └─ node_modules/snapdragon/lib/utils.js (2x)
    4 ms                                │       │                   └─ node_modules/source-map-resolve/lib/source-map-resolve-node.js
   17 ms                                │       └─ node_modules/micromatch/lib/compilers.js (2x)
    8 ms                                │           └─ node_modules/extglob/index.js (4x)
    4 ms                                │               └─ node_modules/extglob/lib/compilers.js
   49 ms                                └─ node_modules/chokidar/lib/fsevents-handler.js
   45 ms                                    └─ node_modules/fsevents/fsevents.js (5x)
   41 ms                                        └─ node_modules/fsevents/node_modules/node-pre-gyp/lib/node-pre-gyp.js (6x)
   27 ms                                            ├─ node_modules/fsevents/node_modules/npmlog/log.js (6x)
   12 ms                                            │   ├─ node_modules/fsevents/node_modules/are-we-there-yet/index.js (3x)
   11 ms                                            │   │   └─ node_modules/fsevents/node_modules/are-we-there-yet/tracker-group.js
    7 ms                                            │   │       └─ node_modules/fsevents/node_modules/are-we-there-yet/tracker-stream.js (3x)
    6 ms                                            │   │           └─ node_modules/fsevents/node_modules/readable-stream/readable.js (5x)
   12 ms                                            │   └─ node_modules/fsevents/node_modules/gauge/index.js (8x)
    9 ms                                            │       └─ node_modules/fsevents/node_modules/gauge/plumbing.js (2x)
    6 ms                                            │           └─ node_modules/fsevents/node_modules/gauge/render-template.js (6x)
    8 ms                                            ├─ node_modules/fsevents/node_modules/node-pre-gyp/lib/util/napi.js (2x)
    7 ms                                            │   └─ node_modules/fsevents/node_modules/rimraf/rimraf.js
    4 ms                                            └─ node_modules/fsevents/node_modules/node-pre-gyp/lib/pre-binding.js (5x)

@fdintino fdintino merged commit cefb1cf into mozilla:master Oct 17, 2019
@fdintino
Copy link
Collaborator

LGTM. I've merged this. Thanks!

@eklingen
Copy link
Contributor Author

Cool! 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants