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 🙏

© 2024 – Pkg Stats / Ryan Hefner

lazy-getter

v1.0.0

Published

[![npm version](https://badge.fury.io/js/lazy-getter.svg)](https://badge.fury.io/js/lazy-getter) [![GitHub version](https://badge.fury.io/gh/briandamaged%2Fnode-lazy-getter.svg)](https://badge.fury.io/gh/briandamaged%2Fnode-lazy-getter)

Downloads

14

Readme

lazy-getter

npm version GitHub version

Lazily evaluate properties and cache the results.

Installation

npm install lazy-getter

Usage

Plain-Old Javascript

If you're using plain-old Javascript, then you can inject lazy getters into your objects via the injectLazyGetter(...) function:

const { injectLazyGetter } = require('lazy-getter');

const x = {};

// Notice: this getter is computationally expensive to run!
injectLazyGetter(x, "expensive", function() {
  console.log("Starting calculations");
  let k = 0;
  for(let i = 0; i < 100000; ++i) {
    for(let j = 0; j < 100000; ++j) {
      ++k;
    }
  }

  console.log("Finished expensive calculation!");
  return k;
});


// The getter will be evaluated the first time it is invoked
console.log(x.expensive);  // Super SLOW!


// Now the result is cached.  So, all subsequent calls will
// just be regular property lookups.
console.log(x.expensive);  // Super FAST!

Decorators

If you're using Babel's Decorators Transform, then you can convert your getters to lazy getters via the @lazyGetter decorator:

const { lazyGetter } = require('lazy-getter');

const x = {
  @lazyGetter
  get expensive() {
    console.log("Starting calculations");
    let k = 0;
    for(let i = 0; i < 100000; ++i) {
      for(let j = 0; j < 100000; ++j) {
        ++k;
      }
    }

    console.log("Finished expensive calculation!");
    return k;
  },
};

console.log(x.expensive); // Super SLOW!
console.log(x.expensive); // Super FAST!

...Why?

Lazy Getters might seem a bit esoteric, but they are definitely very handy in certain scenarios. Here are a few examples:

Rarely-needed, but Computationally-Expensive Property

For example:

class HumongousNumber {

  @lazyGetter
  get primeFactorization() {
    // Algorithm that returns an Array of prime factors
    // for the number.  Might take years to run.
  }
}

Infinite Object Graphs

Sometimes, it's useful to model a problem as an infinite graph of Objects. Obviously, you can't actually contruct this infinite graph since it would require an unlimited about of time and memory. Fortunately, you can "fake it" using lazy getters:


function integerNode(i) {
  return {
    value: i,

    @lazyGetter
    get next() {
      return integerNode(i + 1);
    },
  }
}

const x = integerNode(0);

console.log(x.next.next.next.next.value); // Prints: 4

Now the object graph will be constructed as needed.

FYI: zelda-lists leverages this technique to convert any iterable Object into a linked list. This allows infinite iterators to be converted into infinite object graphs, which makes it significantly easier to implement recursive algorithms.