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

memoize-weak

v1.0.2

Published

Garbage-collected memoizer for variadic functions

Downloads

121,416

Readme

memoize-weak

npm version Stability Build Status

Garbage-collected memoizer for variadic functions

Installation

npm install memoize-weak

Example

import memoize from 'memoize-weak';

let foo = { foo: true };
let bar = { bar: true };
let baz = { baz: true };

const fn = memoize((...args) => args); // Create a memoized function

fn(foo, bar, baz); // Returns [{ foo: true }, { bar: true }, { baz: true }]
fn(foo, bar, baz); // Returns cached result

foo = bar = baz = undefined; // Original foo, bar and baz are now eligible for garbage collection

Features

  • Memoizes multiple arguments of any type
  • Previous arguments are automatically garbage-collected when no longer referenced elsewhere
  • No external dependencies
  • Compatible with ES5 and up

How does memoize-weak differ from other memoize implementations?

Memoize functions cache the return value of a function, so that it can be used again without having to recalculate the value.

They do this by maintaining a cache of arguments that the function has previously been called with, in order to return results that correspond to an earlier set of arguments.

Usually this argument cache is retained indefinitely, or for a predefined duration after the original function call. This means that any objects passed as arguments are not eligible for garbage collection, even if all other references to these objects have been removed.

memoize-weak uses "weak references" to the argument values, so that once all the references to the arguments have been removed elsewehere in the application, the arguments will become eligible for cleanup (along with any cached return values that correspond to those arguments).

This allows you to use memoized functions with impunity, without having to worry about potential memory leaks.

Using memoize-weak in ES5 applications

memoize-weak requires that Map and WeakMap are globally available. This means that these will have to be polyfilled for use in an ES5 environment.

Some examples of Map and WeakMap polyfills for ES5: