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-memoize

v2.1.0

Published

Builds a transparent cache for a function. When the cache is outdated, it will be returned until the update function has completed (i.e. callers will never have to wait for updates after initialization.)

Downloads

2,562

Readme

lazy-memoize

Builds a transparent cache for a function. When the cache is outdated, it will be returned until the update function has completed (i.e. callers will never have to wait for updates after initialization.)

Example

const cache = require('lazy-memoize');

async function doSomethingSlow(arg) {
	const result = await somethingSlow();
	return arg+result;
}

const doSomethingSlowCached = cache(doSomethingSlow, 60);

const something = await doSomethingSlowCached(123); //slow;
const somethingAgain = await doSomethingSlowCached(123); //fast;
// ..soon after 60 seconds
const somethingNew = await doSomethingSlowCached(123); // fast, and refreshed.

Usage

interface Cache extends EventEmitter {
	invalidate: () => Promise<void>
}
function<T> cache(f: (A1, A2, ...) => Promise<T>, maxAgeSeconds: number, options?: Options): (typeof f) & Cache

Builds a cacher for function f, which is invalidated and refreshed if called after maxAgeSeconds seconds. The cacher can then be called with no arguments, and returns the result of f.

cachedFunction.invalidate() : Promise<void>

Invalidates the cache so future calls will result in the function being re-evaluated.

Options

errors

The errors option controls cache behaviour if f throws.

errors = 'passthrough' :

If f throws an error and c = cache(f), c() will throw that error until the cache is invalidated and refreshed. c does not emit any events. This is the default behvaiour.

errors = 'swallow' :

If f throws an error and c = cache(f), c() will return the last successful operation before f() threw. The cache will only be marked as fresh if the refresh call of f() does not throw, so future calls of c() will continue to trigger f() regardless of cache age, until f() eventually succeeds. If f() fails but has never previously succeeded, c() will throw whatever f() threw. If a call to f throws, c will emit an error event, which can (and should) be handled with a c.on('error', (e) => { ... } ) listener.

This behaviour is designed for a function that takes a long time and occasionally fails, but in case of failures, you'd rather just try again. For example, a connection to a crappy old SAP server that sometimes times out for no reason.

Breaking Changes

v2.0.0

  • Requires Node >= 8
  • f() must now return a Promise (the carrot to this stick is that your cached functions now share parameter names and types with the wrapped functions, so IDEs will show better autocomplete for cached functions)