All Projects → jonschlinkert → lazy-cache

jonschlinkert / lazy-cache

Licence: MIT license
Cache requires to be lazy-loaded when needed. Uses node's own require system with tried and true, plain-vanilla JavaScript getters.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to lazy-cache

ethjs-contract
A simple contract object for the Ethereum RPC layer.
Stars: ✭ 21 (-58.82%)
Mutual labels:  object
electron-require
Simplified require in electron applications
Stars: ✭ 26 (-49.02%)
Mutual labels:  require
obfc.js
Object Based Flow Charts(obfc): Draws SVG-based flow charts by creating objects in your web pages.
Stars: ✭ 19 (-62.75%)
Mutual labels:  object
xoid
Framework-agnostic state management library designed for simplicity and scalability ⚛
Stars: ✭ 96 (+88.24%)
Mutual labels:  vanilla
object-visit
Call the given method on each value in the given object.
Stars: ✭ 19 (-62.75%)
Mutual labels:  object
pfQuest
A Questhelper and Database Addon for World of Warcraft: Vanilla & TBC
Stars: ✭ 109 (+113.73%)
Mutual labels:  vanilla
wc-codemirror
CodeMirror as a vanilla web component
Stars: ✭ 31 (-39.22%)
Mutual labels:  vanilla
SoftUni-Software-Engineering
SoftUni- Software Engineering
Stars: ✭ 47 (-7.84%)
Mutual labels:  object
reach-schema
Functional schema-driven JavaScript object validation library.
Stars: ✭ 34 (-33.33%)
Mutual labels:  object
obman
[cvpr19] Hands+Objects synthetic dataset, instructions to download and code to load the dataset
Stars: ✭ 120 (+135.29%)
Mutual labels:  object
js-deep-sort-object
Simple module to sort objects recursively by its keys
Stars: ✭ 19 (-62.75%)
Mutual labels:  object
tradeship
Automatically imports missing JavaScript dependencies and removes unused ones.
Stars: ✭ 42 (-17.65%)
Mutual labels:  require
DTE
Generate C# class from database table
Stars: ✭ 26 (-49.02%)
Mutual labels:  object
has-value
Returns true if a value exists, false if empty. Works with deeply nested values using object paths.
Stars: ✭ 27 (-47.06%)
Mutual labels:  object
js-explorer
Find the method you need without digging through the docs, directly on the command line!
Stars: ✭ 287 (+462.75%)
Mutual labels:  object
curved-menu
VanillaJS fully configurable curved menu (circular navigation)
Stars: ✭ 30 (-41.18%)
Mutual labels:  vanilla
omit-empty
Recursively omit empty properties from an object. Omits empty objects, arrays, strings, and optionally zero. Similar results to what you would expect with `compact` for arrays.
Stars: ✭ 71 (+39.22%)
Mutual labels:  object
morrowind-sharp
A Morrowind modding guide. New and experienced players welcomed.
Stars: ✭ 107 (+109.8%)
Mutual labels:  vanilla
php-helpers
An extensive set of PHP helper functions and classes.
Stars: ✭ 27 (-47.06%)
Mutual labels:  object
derivejs
DeriveJS is a reactive ODM - Object Document Mapper - framework, a "wrapper" around a database, that removes all the hassle of data-persistence by handling it transparently in the background, in a DRY manner.
Stars: ✭ 54 (+5.88%)
Mutual labels:  object

lazy-cache NPM version NPM monthly downloads NPM total downloads Linux Build Status

Cache requires to be lazy-loaded when needed.

Install

Install with npm:

$ npm install --save lazy-cache

Heads up!

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:

// in the main export of a library, if you do this it will
// automatically modify the String.prototype _globally_, 
// the moment node.js loads the dependency tree
String.prototype.foo = function() {};

// same if you do something like this
// (dont' do this, ever. wrap this kind of code in a function
// and allow implementors to decide when to call it)
while (foo) {
  // do stuff
}

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 requires 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.

Usage

var 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.

var utils = require('lazy-cache')(require);

// `npm install glob`
utils('glob');

// glob sync
console.log(utils.glob.sync('*.js'));

// glob async
utils.glob('*.js', function (err, files) {
  console.log(files);
});

Use as a function

var utils = require('lazy-cache')(require);
var glob = utils('glob');

// `glob` is a now a function that may be called when needed
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);

// alias `ansi-yellow` as `yellow`
utils('ansi-yellow', 'yellow');
console.log(utils.yellow('foo'));

Dot notation may also be used in the alias to create an object hierarchy.

Example

var utils = require('lazy-cache')(require);
utils('ansi-cyan', 'color.cyan');
utils('ansi-yellow', 'color.yellow');
utils('ansi-magenta', 'color.magenta');
console.log(utils.color.cyan('foo'));
console.log(utils.color.yellow('bar'));
console.log(utils.color.magenta('baz'));

Browserify usage

Example

var utils = require('lazy-cache')(require);
// temporarily re-assign `require` to trick browserify
var fn = require;
require = utils;
// list module dependencies (here, `require` is actually `lazy-cache`)
require('glob');
require = fn; // restore the native `require` function

/**
 * Now you can use glob with the `utils.glob` variable
 */

// sync
console.log(utils.glob.sync('*.js'));

// async
utils.glob('*.js', function (err, files) {
  console.log(files.join('\n'));
});

Kill switch

To force lazy-cache to immediately invoke all dependencies, do:

process.env.UNLAZY = true;

About

Related projects

lint-deps: CLI tool that tells you when dependencies are missing from package.json and offers you a… more | homepage

Contributing

Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.

Contributors

Commits Contributor
31 jonschlinkert
27 doowb

Building docs

(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:

$ npm install -g verb verb-generate-readme && verb

Running tests

Install dev dependencies:

$ npm install -d && npm test

Author

Jon Schlinkert

License

Copyright © 2016, Jon Schlinkert. Released under the MIT license.


This file was generated by verb-generate-readme, v0.2.0, on November 07, 2016.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].