From 21303cecb5798405d63957608d89bc7113464459 Mon Sep 17 00:00:00 2001 From: Ben Gotow Date: Tue, 17 May 2016 18:37:20 -0700 Subject: [PATCH] fix(nylas-exports): Avoid repeatedly calling require inside lazyLoad --- src/global/nylas-exports.coffee | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/global/nylas-exports.coffee b/src/global/nylas-exports.coffee index cc47b7e34..ed25c6979 100644 --- a/src/global/nylas-exports.coffee +++ b/src/global/nylas-exports.coffee @@ -2,6 +2,11 @@ TaskRegistry = require('../task-registry').default StoreRegistry = require('../store-registry').default DatabaseObjectRegistry = require('../database-object-registry').default +# Calling require() repeatedly isn't free! Even though it has it's own cache, +# it still needs to resolve the path to a file based on the current __dirname, +# match it against it's cache, etc. We can shortcut all this work. +RequireCache = {} + class NylasExports @default = (requireValue) -> requireValue.default ? requireValue @@ -10,7 +15,9 @@ class NylasExports @lazyLoad = (prop, path) -> Object.defineProperty @, prop, get: -> - NylasExports.default(require("../#{path}")) + key = "#{prop}#{path}" + RequireCache[key] = RequireCache[key] || NylasExports.default(require("../#{path}")) + return RequireCache[key] enumerable: true @lazyLoadCustomGetter = (prop, get) ->