From 22d9f20dee4aa73aab81a9642edc98ee5fc20868 Mon Sep 17 00:00:00 2001 From: Ben Gotow Date: Tue, 13 Feb 2018 23:40:31 -0800 Subject: [PATCH] =?UTF-8?q?Composer=20perf:=20preload=20components=20to=20?= =?UTF-8?q?avoid=20parsing=20javascript=20on=20=E2=80=9Creply=E2=80=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/global/mailspring-component-kit.es6 | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/app/src/global/mailspring-component-kit.es6 b/app/src/global/mailspring-component-kit.es6 index 1dc630bc84..3fe739be72 100644 --- a/app/src/global/mailspring-component-kit.es6 +++ b/app/src/global/mailspring-component-kit.es6 @@ -5,6 +5,24 @@ // `require` files the first time they're called. module.exports = exports = {}; +// Because requiring files the first time they're used hurts performance, we +// automatically load components slowly in the background using idle cycles. +setTimeout(() => { + const remaining = Object.keys(module.exports); + const fn = deadline => { + let key = null; + let bogus = 0; // eslint-disable-line + while ((key = remaining.pop())) { + bogus += module.exports[key] ? 1 : 0; + if (deadline.timeRemaining() <= 0) { + window.requestIdleCallback(fn, { timeout: 5000 }); + return; + } + } + }; + window.requestIdleCallback(fn, { timeout: 5000 }); +}, 500); + const resolveExport = requireValue => { return requireValue.default || requireValue; };