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

memo-decorator

v2.0.1

Published

Decorator for caching the results of your method calls using lodash.memoize

Downloads

38,945

Readme

Build Status

Memo Decorator

This decorator applies memoization to a method of a class.

Usage

Apply the decorator to a method of a class. The cache is local for the method but shared among all instances of the class. Strongly recommend you to use this decorator only on pure methods.

Installation:

npm i memo-decorator --save

Configuration

export interface Config {
  resolver?: Resolver;
  cache?: MapLike;
}
  • Resolver is a function, which returns the key to be used for given set of arguments. By default, the resolver will use the first argument of the method as the key.
  • MapLike is a cache instance. By default, the library would use Map.

Example:

import memo from 'memo-decorator';

class Qux {
  @memo({
    resolver: (...args: any[]) => args[1],
    cache: new WeakMap()
  })
  foo(a: number, b: number) {
    return a * b;
  }
}

Demo

import memo from 'memo-decorator';

class Qux {
  @memo()
  foo(a: number) {
    console.log('foo: called');
    return 42;
  }

  @memo({
    resolver: _ => 1
  })
  bar(a: number) {
    console.log('bar: called');
    return 42;
  }
}

const a = new Qux();
// Create a new cache entry and associate `1` with the result `42`.
a.foo(1);
// Do not invoke the original method `foo` because there's already a cache
// entry for the key `1` associated with the result of the method.
a.foo(1);
// Invoke the original `foo` because the cache doesn't contain an entry
// for the key `2`.
a.foo(2);

// Invoke `bar` and return the result `42` gotten from the original `bar` implementation.
a.bar(1);
// Does not invoke the original `bar` implementation because of the specified `resolver`
// which is passed to `memo`. For any arguments of the function, the resolver will return
// result `1` which will be used as the key.
a.bar(2);

const b = new Qux();
// Does not invoke the method `foo` because there's already an entry
// in the cache which associates the key `1` to the result `42` from the
// invocation of the method `foo` by the instance `a`.
b.foo(1);

// Outputs:
// foo: called
// foo: called
// bar: called

License

MIT