Skip to content

Instantly share code, notes, and snippets.

@FokkeZB
Last active December 16, 2015 21:39
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FokkeZB/5501314 to your computer and use it in GitHub Desktop.
Save FokkeZB/5501314 to your computer and use it in GitHub Desktop.
Lazy exporting expensive properties
// In-expensive property
exports.foo = 'bar';
// Expensive property
var propertyName;
Object.defineProperty(exports, "propertyName", {
get: function() {
return propertyName = propertyName || require('someOtherModule');
}
});

If you have a CommonJS module that exposes properties that are expensive to create (e.g. other CommonJS modules or some external resource), you can load them lazy using Object.defineProperty().

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