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

easy-memoize

v1.0.12

Published

Memoization made easy

Downloads

36

Readme

easy-memoize

Build Status Coverage Status

Memoization is a simple thing and should be made easy to implement. The library easy-memoize will help you with it. For an enhanced documentation please visit the wiki pages.

Installation

The package can be installed using npm or yarn. For example:

npm install --save easy-memoize

Usage

The usage of this library is as easy as 1, 2, 3. Just wrap it around your called function and pass in the arguments, like you normally would.

import easyMemo from "easy-memoize";

/* ... */

// On first call calculates the result of 1 * 2, if this code ever gets called again, the cached result will be returned.
easyMemo((a, b) => a * b, [])(1, 2); // returns: 2

// It returns the same object if the dependency is the same ( === safe)
easyMemo((value) => ({ value, randomProp: "abc" }), [])("R3DST0RM"); // returns: { value: "R3DST0RM", randomProp: "abc" }

Memoize Signature

The memoize function receives two input params, the function to memoize as well as an array of dependencies. When those dependencies change, the function will be executed again otherwise a cached result will be returned.

E.g: easyMemo(() => { return anotherFunction() }, [anotherFunction])

If anotherFunction changes, the memoized function will be executed again.

Memoize Limits

The standard cache size of one function is at 10. Means, 10 results will be cached. If a function exceeds this limit, the oldest results will be removed.

Using overrideMaxCacheSize(num: Number) allows you to override this default behavior.

How it works

The memoization is done by storing the function and its arguments. If one of it changes, the function will be executed again.

Let's assume there is a function called: heavyCalculation with the following implementation:

const fibonacci = (num) => num <= 1 ? 1 : fibonacci(num - 1) + fibonacci(num - 2);

const heavyCalculation = () => fibonacci(40);

The bet is, you would not want it to run again and again everytime the result is needed, just once, until something changes.

Using easy-memoize, this would be achieved wrapping heavyCalculation with the memoize function:

const easyHeavyCalculation = easyMemo(heavyCalculation, []) // returns a new memoized function

// By running easyHeavyCalculation(); a cached value will be returned if it gets executed a second time
console.log(easyHeavyCalculation());
console.log(easyHeavyCalculation()); // returns cached value

Motivation

As motivation served the useCallback function from React. Where it is possible to memoize a function call based on it's dependencies.

Therefore this library strives to be as efficient as possible while maintain the easiness of useCallback.

Contribute

Your contribution is highly wanted. If there is a feature or issue you want to work an feel free to submit a PR.

Browser Support

We care about browser support. Therefore this library has support for Internet Explorer 11

Chrome | Firefox | Safari | Opera | Edge | IE | --- | --- | --- | --- | --- | --- | Latest ✔ | Latest ✔ | Latest ✔ | Latest ✔ | Latest ✔ | 11 ✔ |

Donate

If you like this library and would like to support this work feel free to donate here:

paypal

License

Licensed under MIT License