
Security News
Open Source CAI Framework Handles Pen Testing Tasks up to 3,600× Faster Than Humans
CAI is a new open source AI framework that automates penetration testing tasks like scanning and exploitation up to 3,600× faster than humans.
lazy-initializer
Advanced tools
Transparent generic deferred initializer which waits until your first-time use.
Lazy Initializer is a generic deferred object initializer, which will creates a wrapper which waits for your first time use, then it will triggers the initialize function you defined. The concept is similar to C#'s Lazy class, but more transparent implementation in ES6.
Simple usage for wrapping a property in a class:
import { LazyProperty } from 'lazy-initializer'; // or require(...) if your environment does not support import.
class Schrodinger {
@LazyProperty
get cat() { return Math.random() > 0.5; }
// Setter will be called when the value has been assigned first time.
// Setters can be not defined, but then the property will be read-only.
set cat(value) {
console.log(`It is ${value ? 'alive' : 'dead'} now!`);
assert.strictEqual(value, this.cat);
}
}
const isAlive = new Schrodinger().cat;
Alternatively, if your transpiler or environment does not support ES6 decorators:
import { LazyProperty } from 'lazy-initializer';
class Schrodinger {
get cat() { return Math.random() > 0.5; }
}
LazyProperty.transform(Schrodinger, 'cat');
Also, you may manually craete a new lazy property without defining the getter/setter before:
import { LazyProperty } from 'lazy-initializer';
const someObject = {};
LazyProperty.define(someObject, 'somelazyField', () => 'boo!');
// Then, `someObject` has a `somelazyField` now!
// You may batch define more properties like this:
LazyProperty.define(someObject, {
someOtherLazyField: () => 'another one!',
someMoreComplicatedLazyField: {
init: () => 'More controllable behaviour!',
enumerable: false,
configurable: false,
writable: true,
},
});
Another advanced usage is wrapping a whole object (which uses proxy):
import { LazyProxy } from 'lazy-initializer';
const somethingExpensive = LazyProxy.create(() => {
// Some heavy stuffs...
return someHeavyObject;
});
// You may treat the object is loosely equals to the initialized object itself.
const someValue = somethingExpensive.someValue();
If the lazy initialized object will be used as constructors:
import { LazyProxy } from 'lazy-initializer';
const SomeHeavyConstructor = LazyProxy.create(() => {
// Some heavy stuffs...
return Foo;
}, true);
// The true means this will use as constructor,
// the proxy internals will do some tweaks to make this to be supported.
const someValue = new SomeHeavyConstructor();
For more information, please see docs.
In your Node.js project path, run:
$ npm install --save lazy-initializer
or yarn
$ yarn add lazy-initializer
This module make uses the new ES6 features, especially proxy, therefore it requires at least Node.js 6+ to works.
ECMAScript 6 compatibility table
FAQs
Transparent generic deferred initializer which waits until your first-time use.
We found that lazy-initializer demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
CAI is a new open source AI framework that automates penetration testing tasks like scanning and exploitation up to 3,600× faster than humans.
Security News
Deno 2.4 brings back bundling, improves dependency updates and telemetry, and makes the runtime more practical for real-world JavaScript projects.
Security News
CVEForecast.org uses machine learning to project a record-breaking surge in vulnerability disclosures in 2025.