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

simple-async-memo

v0.0.2

Published

Minimalistic, fast async memoization library with lazy cache renewal.

Downloads

7

Readme

Travis Build Status npm version License: MIT

simple-async-memo

A minimalistic, reasonably fast (see alternatives below) memoization library with lazy cache renewal specifically for Promise-based usage.

Rejection is generally considered an unacceptable result. Hence, whenever calling the function to be memoized is required (either the initial call or on cache expiration - see maxAge), simple-async-memo will retry on every call (but you can also customize the interval at which this happens - see rejectRetryDelay).

If the initial call results in rejection, this will still be returned though.

Installation

yarn add simple-async-memo

Example

const {memoize} = require('simple-async-memo');

function fn(arg1, arg2) {
    return fetch('...');
}

const memoized = memoize(fn); // fn HAS to return a Promise!

memoized('foo', 'bar')
    .then(...)
    .catch(...);
memoized('foo', 'bar')
    .then(...)
    .catch(...);

Options

maxAge

Default: MAX_SAFE_INTEGER * 0.5

So by default, the cache doesn't expire.

rejectRetryDelay

Default: 10000 ms

This does not mean that simple-async-memo keeps retrying by itself. Instead, if you e.g. call a function regularly every 200 ms and you set this delay to 500 ms, if the initial call failed, simple-async-memo will not retry for the first 2 calls but only the 3rd. IF that one succeeds, the cache will be updated lazily - meaning, only the 4th call will get the resolved value.

matchesKey: (any[], any[]) => boolean

Default: shallow comparison of all array elements

Benchmark & Alternatives

You can run the benchmark by cloning this repo and doing npm run benchmark.

The basis is a function that takes two arguments and returns a Promise. For those libraries that support it, the "promise" flag was set.

| Name | Ops/sec. | Relative margin of error | Sample size | | ----------------- | ---------- | ------------------------ | ----------- | | moize | 11,354,469 | ± 1.04% | 81 | | simple-async-memo | 5,717,515 | ± 0.65% | 85 | | memoizee | 1,707,028 | ± 1.83% | 79 | | fast-memoize | 971,844 | ± 0.74% | 86 |

Two parameters

Moize offers great performance and has async options but unfortunately no lazy cache renewal. And its architecture is structured in a way that doesn't allow for contributing such a feature easily. (If this changes, let me know. 😉)