Proxies for lazy loading expensive objects. Lazy + Proxy === Laxy
$ npm install laxy
const laxy = require('laxy'); const proxy = laxy(generatorFn)(...argsToPass); // generatorFn will only be called once, but not until you interact with the proxy in some way: proxy(); proxy.foo; Object.keys(proxy); // etc... // Can be used as a lazy require const _ = laxy(require)('lodash'); // lodash won't be loaded until you do something with it: _.isNumber(3);
The function that should be called to generate the object being proxied. This function will be called lazily and only once.
Any number of arguments may be provided and will be…
Laxy exports a single function that looks like this:
I think the easiest way to explain how it works, is with an example. Assume this is running in Node:
const laxy = require("laxy"); const fs = laxy(require, "fs");
fs module will be loaded, only when it is used.
require is called with
"fs" as the argument, the first time you try to use
When you call
laxy, it immediately returns an object. This object is a Proxy. The first time you use that object in any way that is supported by a Proxy handler,
fn is called, and the object starts behaving exactly like the object returned by
I used it to dramatically speed up the cold boot time of Google Cloud Functions for Firebase. Each function gets its own "container", but that container also includes the code for all the other functions, meaning I was loading dependancies for every function, not just the on in the current container. So I wrapped all dependancies in
laxy, so they're only loaded the first time they're used.