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 🙏

© 2026 – Pkg Stats / Ryan Hefner

instance-memoize

v1.0.0

Published

[![Greenkeeper badge](https://badges.greenkeeper.io/akito0107/instance-memoize.svg)](https://greenkeeper.io/)

Readme

instance-memoize

Greenkeeper badge

npm version CircleCI Maintainability Test Coverage

flexible cache wrapper for js object.

Getting Started

Prerequisites

  • Node.js v8+
  • npm or yarn

Installing

$ npm add instance-memoize

Usage

basic example

instance-memoize cached results of method invocation on the specified object permanently. If you want to manage cache purging and lifetime see [custom cache example](#with custom cache).

import memoize from 'instance-memoize'; // or const memoize = require('instance-memoize') for commonjs modules

const obj = {
  calledCounter: 0,
  method: function() {
    calledCounter++;
    return "test value";
  }
};

const wrapped = memoize({
  instance: obj,
  methods: ['method']
})

console.log(wrapped.method())      // output => "test value"
console.log(wrapped.calledCounter) // output => "1"

/*
  The result of "method" is stored internal cache,
  From the second call, the result is returned via cache.
 */
console.log(wrapped.method())      // output(from cache) => "test value"  
console.log(wrapped.calledCounter) // output => "1"

with es2015 classes

import memoize from 'instance-memoize'; // or const memoize = require('instance-memoize') for commonjs modules

class Sample {
  constructor() {
    this.calledCounter = 0;
  }
  someMethod() {
    this.calledCounter++;
    return "value";
  }
}

const wrapped = memoize({
  instance: new Sample(),
  methods: ['someMethod']
})

console.log(wrapped.someMethod())      // output => "test value"
console.log(wrapped.calledCounter)     // output => "1"

console.log(wrapped.someMethod())      // output(from cache) => "test value"  
console.log(wrapped.calledCounter)     // output => "1"

with async functions

instance-memoize also works with async/await or Promise.

import memoize from 'instance-memoize'; // or const memoize = require('instance-memoize') for commonjs modules

class Sample {
  constructor() {
    this.calledCounter = 0;
  }
  async someMethod() {
    this.calledCounter++;
    return "value";
  }
}

const wrapped = memoize({
  instance: new Sample(),
  methods: ['someMethod']
})

console.log(await wrapped.someMethod())   // output => "test value"
console.log(wrapped.calledCounter)        // output => "1"

console.log(await wrapped.someMethod())   // output(from cache) => "test value"  
console.log(wrapped.calledCounter)        // output => "1"

with custom cache

You can use your customized cache by passing cache instance via options. cache instance must implement three methods;

  • get(key: string) :Object
  • set(key: string, obj: any)
  • reset()

This example shows instance-memoize working with isaacs's node-lru-cache.

import memoize from 'instance-memoize'; // or const memoize = require('instance-memoize') for commonjs modules
import LRU from 'lru-cache' // import isaacs's node-lru-cache

class Sample {
  constructor() {
    this.calledCounter = 0;
  }
  someMethod() {
    this.calledCounter++;
    return "value";
  }
}

const wrapped = memoize({
  instance: new Sample(),
  methods: ['someMethod'],
  options: {
    cache: LRU({ ...lruCacheOptions })
  }
})

API

memoize(instance, [...methodNames], options)

arguments

  • instance (Object): Source object that wrapped to.
  • methodNames (Array): Method name(s) that cached.
  • options (Object):
    • cache (Object): Custom Cache object
    • keygen (function(methodName: string, ...args): string ): cache key generator function

returns

(Object): Wrapped instance that caches the result of specified method invocation.

Authors

License

This project is licensed under the Apache2.0 License - see the LICENSE.md file for details