Gathering detailed insights and metrics for lazy-cache
Gathering detailed insights and metrics for lazy-cache
Gathering detailed insights and metrics for lazy-cache
Gathering detailed insights and metrics for lazy-cache
Cache requires to be lazy-loaded when needed. Uses node's own require system with tried and true, plain-vanilla JavaScript getters.
npm install lazy-cache
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
51 Stars
62 Commits
12 Forks
3 Watching
1 Branches
2 Contributors
Updated on 14 Sept 2022
JavaScript (100%)
Cumulative downloads
Total Downloads
Last day
0.6%
1,208,638
Compared to previous day
Last week
7.2%
7,095,358
Compared to previous week
Last month
28.8%
25,963,532
Compared to previous month
Last year
-4.8%
265,259,353
Compared to previous year
1
Cache requires to be lazy-loaded when needed.
Install with npm:
1$ npm install --save lazy-cache
It's suprising how many libraries are in the average dependency tree that don't belong there for one reason or another. Either because they were accidentally listed as dependencies
instead of devDepedencies
, or they are required in a file as variables, but the variable is never actually used (poor linting), and so on. Or because the maintainer made the decision to add the deps, even though they will never (or can't ever) be used by 99.9% of users.
Worse, many libraries like chalk and shelljs actually execute code when require()
is called!? (shelljs was modifying the String.prototype
, and chalk loops over some objects to dynamically create methods). In other words, they do something like this:
1// in the main export of a library, if you do this it will 2// automatically modify the String.prototype _globally_, 3// the moment node.js loads the dependency tree 4String.prototype.foo = function() {}; 5 6// same if you do something like this 7// (dont' do this, ever. wrap this kind of code in a function 8// and allow implementors to decide when to call it) 9while (foo) { 10 // do stuff 11}
In any case, just having these libraries in your dependency tree somewhere means that their code will excecute the moment you run your application even if the libraries are never called by your application or any other code anywhere in the tree.
solution
lazy-cache doesn't use any "magic", it uses native, plain-vanilla, tried and true javascript getters to call node's require()
system.
Faster, safer code
There many advantage to this, the main is that require
s are loaded on demand, so only code that is actually used will ever be loaded. As a result, applications will load faster (sometimes much faster - we've seen load times drop from ~1 second to less than 50 milliseconds).
Moreover, in some cases this also avoids inadvertently loading libraries that execute code or modifies globals, etc.
webpack users
If you use webpack and are experiencing issues with lazy-cache, this is a known bug caused by webpack, not lazy-cache. There is a solution though, you can use unlazy-loader, a webpack loader that fixes the webpack bug.
1var utils = 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.
1var utils = require('lazy-cache')(require); 2 3// `npm install glob` 4utils('glob'); 5 6// glob sync 7console.log(utils.glob.sync('*.js')); 8 9// glob async 10utils.glob('*.js', function (err, files) { 11 console.log(files); 12});
Use as a function
1var utils = require('lazy-cache')(require); 2var glob = utils('glob'); 3 4// `glob` is a now a function that may be called when needed 5glob().sync('foo/*.js');
An alias may be passed as the second argument if you don't want to use the automatically camel-cased variable name.
Example
1var utils = require('lazy-cache')(require); 2 3// alias `ansi-yellow` as `yellow` 4utils('ansi-yellow', 'yellow'); 5console.log(utils.yellow('foo'));
Dot notation may also be used in the alias to create an object hierarchy.
Example
1var utils = require('lazy-cache')(require); 2utils('ansi-cyan', 'color.cyan'); 3utils('ansi-yellow', 'color.yellow'); 4utils('ansi-magenta', 'color.magenta'); 5console.log(utils.color.cyan('foo')); 6console.log(utils.color.yellow('bar')); 7console.log(utils.color.magenta('baz'));
Example
1var utils = require('lazy-cache')(require); 2// temporarily re-assign `require` to trick browserify 3var fn = require; 4require = utils; 5// list module dependencies (here, `require` is actually `lazy-cache`) 6require('glob'); 7require = fn; // restore the native `require` function 8 9/** 10 * Now you can use glob with the `utils.glob` variable 11 */ 12 13// sync 14console.log(utils.glob.sync('*.js')); 15 16// async 17utils.glob('*.js', function (err, files) { 18 console.log(files.join('\n')); 19});
To force lazy-cache to immediately invoke all dependencies, do:
1process.env.UNLAZY = true;
lint-deps: CLI tool that tells you when dependencies are missing from package.json and offers you a… more | homepage
Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.
Commits | Contributor |
---|---|
31 | jonschlinkert |
27 | doowb |
(This document was generated by verb-generate-readme (a verb generator), please don't edit the readme directly. Any changes to the readme must be made in .verb.md.)
To generate the readme and API documentation with verb:
1$ npm install -g verb verb-generate-readme && verb
Install dev dependencies:
1$ npm install -d && npm test
Jon Schlinkert
Copyright © 2016, Jon Schlinkert. Released under the MIT license.
This file was generated by verb-generate-readme, v0.2.0, on November 07, 2016.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
Reason
Found 1/26 approved changesets -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
security policy file not detected
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn Moreunlazy-loader
Webpack loader to transform lazy-cache files into unlazy cached files.
@pietal.dev/cache
simple, zero-dependency, in-memory, lazy cache for javascript
react-lazy-cache
A utility to lazily calculate and cache values in a react component based on props
flat-cache
A simple key/value storage using files to persist the data