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

@5app/memoize

v2.0.0

Published

Memoize decorator

Downloads

593

Readme

Memoize

CircleCI

In computing, memoization or memoisation is an optimization technique used primarily to speed up computer programs by storing the results of expensive function calls and returning the cached result when the same inputs occur again. Memoization has also been used in other contexts (and for purposes other than speed gains), such as in simple mutually recursive descent parsing.[1] Although related to caching, memoization refers to a specific case of this optimization, distinguishing it from forms of caching such as buffering or page replacement. In the context of some logic programming languages, memoization is also known as tabling.[2]

https://en.wikipedia.org/wiki/Memoization

Usage

!Warning contrived example ahead...

import got from 'got'; // simple http requst library for the purpose of demonstration
import memoize from '@5app/memoize';

// Let's decorate the got function
const memoGot = memoize(got);

// Simultaneously open two connections...
const link = 'https://github.com';
Promise.all([memoGot(link), memoGot(link)];

// Will call...
// GET https://github.com
// ... but that's it, it wont call it again the second request will piggy back off the first.

Options memoize(handler, {...options})

  • option.useCache (Boolean|Function): A truthy/fasly or a function to decide whether to use the cached record or not. Default true
  • option.staleInMs Number: The number of milliseconds before the cache is deemed stale. Results will still be served from the cache whilst an attempt to refresh the cache is made separatly. Default 10000 ms.
  • option.getKey Function: A function to create a key based upon the input of the function being memoized. Default: a serialization of all the arguments.
  • option.cache Object: Instance of a Map like object to store the cache. Default new Map
  • options.cacheMaxSize Number: The maximum number of entries to store in the cache. Default 1000

option.useCache

Whether to use cache this can be a Boolean value (useful to disable it when testing). Or a function e.g.

This snippet checks the cached value before deciding whether to use it...

import memoize from '@5app/memoize';

const memoGot = memoize(got, {
	/**
 	 * @param {object} cached_response - Cached Object
	 * @param {number} cached_response.timestamp - Timestamp when request resolved
	 * @param {string} cached_response.status - 'pending', 'fullfilled', 'rejected'
	 * @param {Promise<*>} cached_response.value - Promise of the request
	 * @returns {Boolean}
	 */
	useCache({timestamp, status}) {
		// Set an expiry on the cache.
		// 2xx, 3xx response last for a full minute before being reused
		// 4xx, 5xx last only a second...
		const age = value.statusCode >= 400 ? 1000 : 60000;

		// Return true if the cache is un-expired, else false.
		return timestamp > Date.now() - AGE;
	}
}

// ...

// Use Memogot
// const req = memogot('link');
// ...

Globally Disable

In testing to disable the memoize cache which can cause issues, use the MEMOIZE_DISABLE environment variable, e.g.

MEMOIZE_DISABLE=1

When set to a truthy value the memoize cache will be circumvented.