startup perf: Reduce require() calls

Every require() that we do before the users sees UI reduces startup
time.

I used the following code (added to index.js) to log every require()
call in the main process:

```js
var Module = require('module')
var required = {}
Module.prototype.require = function (orig) {
  return function (id) {
    if (!required[id]) {
      required[id] = true
      console.log(`${id}   (from ${this.filename})`)
    }
    return orig.apply(this, arguments)
  }
}(Module.prototype.require)
```

From this I was able to learn that lots of modules were being required
that aren't actually used until later.

I also sent this related PR to eliminate another few require()s:
https://github.com/LinusU/node-application-config/pull/4

This increases startup time by 50ms.

We'll probably realize much bigger gains by following this same
procedure for the renderer process.
This commit is contained in:
Feross Aboukhadijeh
2016-09-22 16:33:26 -07:00
parent b8bdf65514
commit a08d576851
6 changed files with 119 additions and 49 deletions

View File

@@ -21,7 +21,6 @@ const app = electron.app
const config = require('../../config')
const log = require('../log')
const menu = require('../menu')
const tray = require('../tray')
function init (state, options) {
if (main.win) {
@@ -81,6 +80,8 @@ function init (state, options) {
})
win.on('close', function (e) {
const tray = require('../tray')
if (process.platform !== 'darwin' && !tray.hasTray()) {
app.quit()
} else if (!app.isQuitting) {
@@ -221,11 +222,15 @@ function toggleFullScreen (flag) {
}
function onWindowBlur () {
const tray = require('../tray')
menu.setWindowFocus(false)
tray.setWindowFocus(false)
}
function onWindowFocus () {
const tray = require('../tray')
menu.setWindowFocus(true)
tray.setWindowFocus(true)
}