What is lazy-cache?
The lazy-cache npm package is designed to lazily require modules, meaning that the modules are only required when they are actually needed. This can help improve the performance of your application by deferring the loading of modules until they are used.
What are lazy-cache's main functionalities?
Lazy Loading of Modules
This feature allows you to defer the loading of a module until it is actually used in your code. In the example, the 'fs' module is not loaded until the 'readFileSync' method is called.
const lazy = require('lazy-cache')(require);
const fs = lazy('fs');
// fs is not loaded until it is used
fs.readFileSync('path/to/file.txt', 'utf8');
Custom Lazy Loading
You can also use lazy-cache to lazily load your own custom modules. In this example, the './customModule' is not loaded until 'someFunction' is called.
const lazy = require('lazy-cache')(require);
const customModule = lazy('./customModule');
// customModule is not loaded until it is used
customModule.someFunction();
Other packages similar to lazy-cache
require-lazy
The require-lazy package provides similar functionality by allowing you to lazily require modules. It offers a simple API for deferring the loading of modules until they are needed, similar to lazy-cache.
proxyquire
Proxyquire is a powerful tool for mocking dependencies during testing, but it also supports lazy loading of modules. It allows you to mock out dependencies and control when they are loaded, providing more flexibility compared to lazy-cache.
import-lazy
Import-lazy is another package that allows you to lazily import modules. It uses ES6 proxies to defer the loading of modules until they are accessed, offering a modern alternative to lazy-cache.
lazy-cache
Cache requires to be lazy-loaded when needed.
Install
Install with npm
$ npm i lazy-cache --save
Usage
var lazy = require('lazy-cache')(require);
Use as a property on lazy
The module is also added as a property to the lazy
function
so it can be called without having to call a function first.
var lazy = require('lazy-cache')(require);
lazy('glob');
console.log(lazy.glob.sync('*.js'));
lazy.glob('*.js', function (err, files) {
console.log(files);
});
Use as a function
var lazy = require('lazy-cache')(require);
var glob = lazy('glob');
glob().sync('foo/*.js');
Aliases
An alias may be passed as the second argument if you don't want to use the automatically camel-cased variable name.
Example
var utils = require('lazy-cache')(require);
utils('ansi-yellow', 'yellow');
console.log(utils.yellow('foo'));
Browserify usage
Example
var lazy = require('lazy-cache')(require);
var fn = require;
require = lazy;
require('glob');
require = fn;
console.log(lazy.glob.sync('*.js'));
lazy.glob('*.js', function (err, files) {
console.log(files.join('\n'));
});
Related
lint-deps: CLI tool that tells you when dependencies are missing from package.json and offers you a… more | homepage
Running tests
Install dev dependencies:
$ npm i -d && npm test
Contributing
Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.
Author
Jon Schlinkert
License
Copyright © 2015 Jon Schlinkert
Released under the MIT license.
This file was generated by verb on December 08, 2015.