gotwarlost/istanbul — pull request #88 by halfninety (2013-08-06 04:06:01+00:00)
Title: Improve speed by not loading unnecessary modules

For a CLI application, many times the user runs the command just
to view help text and usage info, and he would expect these info
to appear really fast. For example, running `git help` only takes
0.012s on my machine, the delay is essentially unnoticeable.

However, in Istanbul, simply showing a help screen requires all
command, store and report plugins to be loaded. Additionally, in
each command, all dependencies for running the command are loaded
even if all we need is to display a help screen.

This commit tries to load modules only when needed. `register-plugins.js`
is removed, each type of plugins are only registered when the
`index.js` of that type is loaded. And in each command plugin,
modules only needed in its `run()` method are loaded in that method,
so calling `synopsis()` and `usage()` doesn't cause these modules
to be loaded.

Note that due to the first change above, now you can't load a plugin
module before the `index.js` of its type is loaded (causes an error
due to circular dependencies). The solution is simply not to directly
load a plugin module. When using a plugin, say `MemoryStore`, don't
use:

```
var MemoryStore = require('./store/memory'),
store = new MemoryStore();
```

Use this instead:

```
var Store = require('./store'),
store = Store.create('memory');
```

This is the change we made to `lib/collector.js`.

Initial testing on my own machine confirms significant speed boosts:

- When installed globally, the time taken for running `istanbul help`
drops by 50%, from 0.33s to 0.16s.
- When installed locally, the time for running the same command drops
by 80%, from 1.3s to 0.26s.