npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

lazy-initializer

v0.3.1

Published

Transparent generic deferred initializer which waits until your first-time use.

Readme

Lazy Initializer

logo GitHub issues GitHub license Node version NPM version NPM downloads

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.

Usage

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.

Installation

In your Node.js project path, run:

$ npm install --save lazy-initializer

or yarn

$ yarn add lazy-initializer

Requirements

This module make uses the new ES6 features, especially proxy, therefore it requires at least Node.js 6+ to works.

ECMAScript 6 compatibility table

License

MIT