Skip to content

Instantly share code, notes, and snippets.

@anaisbetts
Last active April 11, 2019 05:07
Show Gist options
  • Star 40 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save anaisbetts/da85dd246db944c32427d72026192b41 to your computer and use it in GitHub Desktop.
Save anaisbetts/da85dd246db944c32427d72026192b41 to your computer and use it in GitHub Desktop.
Make your Electron apps load faster, with this One Weird Trick
// Include this at the very top of both your main and window processes, so that
// it loads as soon as possible.
//
// Why does this work? The node.js module system calls fs.realpathSync a _lot_
// to load stuff, which in turn, has to call fs.lstatSync a _lot_. While you
// generally can't cache stat() results because they change behind your back
// (i.e. another program opens a file, then you stat it, the stats change),
// caching it for a very short period of time is :ok: :gem:. These effects are
// especially apparent on Windows, where stat() is far more expensive - stat()
// calls often take more time in the perf graph than actually evaluating the
// code!!
// npm install lru-cache first
var lru = require('lru-cache')({max: 256, maxAge: 250/*ms*/});
var fs = require('fs');
var origLstat = fs.lstatSync.bind(fs);
// NB: The biggest offender of thrashing lstatSync is the node module system
// itself, which we can't get into via any sane means.
require('fs').lstatSync = function(p) {
let r = lru.get(p);
if (r) return r;
r = origLstat(p);
lru.set(p, r);
return r;
};
@denkaiyer
Copy link

thanks for this!

@EddieOne
Copy link

image

causes 100% cpu

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment