0

Example 1:

const { app, BrowserWindow, Menu } = require('electron');

Example 2:

const { app, BrowserWindow } = require('electron');

Will example 2 load faster, take less resources and/or work smoother in any way? Or is this practically meaningless? (Assuming you don't actually need to use Menu-related code, of course.)

1 Answers1

0

No, it does not make any difference in performance. Both examples require the whole electron module and all its dependencies.

The only difference is that Menu is not in scope in your module, and that one variable less will be created in memory.

Bergi
  • 513,640
  • 108
  • 821
  • 1,164
  • Oh. Well, then it's more or less just for the programmer to quickly see what he's using in the app, then? One might also say that it's mostly annoying, actually. I frankly don't really see why, if they are all loaded anyway, they have to be defined as separate variables instead of just "electron." or something? – Sam Edgars Nov 06 '17 at 02:58
  • They *are* defined as properties of an object. It's just a syntactic convenience to use [destructuring](https://stackoverflow.com/q/26999820/1048572). The optimisations you are looking for (loading only the things that are needed) are doable with static analysis, I have no idea whether the build process does that. In any case, those optimisation are easier to do when using ES6 import syntax. – Bergi Nov 06 '17 at 03:29
  • My build process consists of taking the Electron dir and putting my assets in there and renaming the .EXE to my program's name. I'm interested in hearing more about "static analysys" if this is possible. – Sam Edgars Nov 06 '17 at 03:37
  • 1
    Some bundlers (like [webpack](https://webpack.js.org/guides/tree-shaking/) or [rollup](https://medium.com/@Rich_Harris/tree-shaking-versus-dead-code-elimination-d3765df85c80)) that compile the whole code into a single file support removing code that isn't needed by applying heuristics. It's useful mostly for serving over the web, reducing the amount of files in a local application usually isn't that much of an issue. – Bergi Nov 06 '17 at 03:49