lazy-modules
lazy-modules implements an easy way to bulk lazy-load Node.js/io.js modules, perfect for implementing in a build system with many imports such as gulp or grunt.
Why?
v8's Script::Compile
, called when require()
is used, usually takes a relatively long time. Multiply this by the 20 packages your Gulpfile.js
imports, times the 10 each of those imports, times 5... you get the point. Just running a linter forces v8 to compile everything, sometimes taking ~5 seconds or worse. If you're impatient like me, this is perfect for you.
This is the time it takes to load and not use vs lazy-load gulpjs/gulp-util:
> var lazy = ;> console; ; console;lazy-load: 6ms> console; gulp_util; console;load: 787ms
If you don't actually use gulp_util
in your Gulpfile, it'll take 6ms to lazy load it. If you do use it, it'll take 787ms to load it - a massive decrease especially when you your task might only take ~100ms.
Install
$ npm i lazy-modules --save
Example
There are three modules in the example
directory: a.js
, b.js
, c.js
. In this example, we'll lazy load them all but only actually run two of them:
var lazy = ;; // we now have access to a, b, and cconsole; // we didn't use c, so it was never loaded
In the same way, it can be used with Gulp packages like so:
var lazy = ;; // if a module has a dash, it is changed to an underscoregulp_util;
API
lazy(glob)
glob
: either aString
orArray
When a string is passed, all modules will be lazy-loaded that match the given glob. If glob
is an Array, they will be mapped over the lazy
function individually.