This package has been deprecated

Author message:

Package no longer supported. Contact Support at https://www.npmjs.com/support for more info.

lazy-modules

1.0.0 • Public • Published

lazy-modules Build Status

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 = require('lazy-modules');
> console.time('lazy-load'); lazy('node_modules/gulp-util'); console.timeEnd('lazy-load');
lazy-load: 6ms
> console.time('load'); gulp_util; console.timeEnd('load');
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 = require('lazy-modules');
lazy('./example/*');
 
// we now have access to a, b, and c
console.log(== 1, b == 2);
 
// 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 = require('lazy-modules');
lazy('./node_modules/gulp-*');
 
// if a module has a dash, it is changed to an underscore
gulp_util.log('gulp-util was lazily loaded!');

API

lazy(glob)

  • glob: either a String or Array

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.

License

MIT license

Dependencies (1)

Dev Dependencies (2)

Package Sidebar

Install

npm i lazy-modules

Weekly Downloads

11

Version

1.0.0

License

MIT

Last publish

Collaborators

  • brendanashworth