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

promise-mem

v1.0.2

Published

Memoizes a promise-returning function

Downloads

2,423

Readme

promise-mem

NPM Version

Memoizes a promise-returning function.

Motivation

After using Sindre Sorhus' p-memoize for a long time, it was time to build an API that was going to handle a ton of load and had to perform many asynchronous calls to fulfill every request. And p-memoize was the choice #1 to manage the load and improve the performance.

But soon after a problem was observed: the initial set of calls that hit the (cold) cache would trigger multiple parallel asynchronous calls until the cache was loaded with data. From that point forward, it handled the calls as expected. As this was not acceptable and debugging p-throttle did not result in any finding, a custom implementation that properly managed that scenario was the next choice: promise-mem.

Install

npm install promise-mem

Usage

const pMemoize = require('promise-mem')

const memoized = pMemoize(asyncCall)

memoized()
  .then(function (value) {
    // `asyncCall` was called and it returned `value`.
  })
  .then(() => memoized())
  .then(function (value) {
    // `asyncCall` was NOT called and `value` was read from the cache.
  })

API

pMemoize(fn, options?)

Returns a memoized function.

fn

Type: Function

A function.

options?

Type: object

options.cache?

Type: object Default: new Map()

The cache storage. Must implement these methods: has(key), set(key, value), get(key) and delete(key)

options.lazy?

Type: boolean Default: true

This flag will force the expiration of the cached value to be computed from the time that value was obtained. If unset, the expiration will be set starting from the request time, without considering the time it takes to get the value into the cache.

options.maxAge?

Type: number Default: Infinity

The maximum amount of time in milliseconds to keep a value in the cache. Expired keys will be deleted from the cache only after the expiration time passes and the key is requested again, forcing a new call to fn. Therefore, expiration is not managed by internally setting a timeout per key as done in other similar libraries.

options.resolver?

Type: Function Default: (...args) => args[0]

Determines how the caching key will be computed. By default, it will only consider the first argument and use strict equality to evaluate a match.

memoizedFn

The memoized version of fn. It resolves to the result of calling fn(), which is called only once per key and during the time the value has to be cached. Rejections are not cached.