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

cached-function

v1.0.0

Published

Wraps a function and caches its return values.

Readme

cached-function

A Node.js module that wraps a function and caches its return value for any given set of arguments. When those same arguments are used again, it pulls from the cache.

  • Cached values can be cleared manually or can be configured to expire automatically.
  • In addition to caching regular functions, the module can also cache class methods and getters.

Installation

npm install cached-function --save

Usage Examples

const CachedFunction = require('cached-function')

function downloadData() {
  console.log('Downloading...')
  // Do the download
  return 'data'
}

// Returns 'data' and outputs "Downloading..."
CachedFunction(downloadData)()

// Returns 'data' but no console output
CachedFunction(downloadData)()

It caches the return value for any given set of arguments:

const CachedFunction = require('cached-function')
let add = CachedFunction((a, b) => a + b)
add(2, 2) // 4
add(2, 2) // returns 4 from the cache

Class Methods

You can easily modify a prototype to cache a particular method for all instantiated objects:

const CachedFunction = require('cached-function')

class TestClass {
  constructor (value) {
    this.value = value
  }

  data (suffix) {
    console.log('The data method was called')
    return this.value + suffix
  }
}

// Cache the data method
TestClass.prototype.data = CachedFunction(TestClass.prototype.data)

const test = new TestClass('value')
test.data(123) // returns 'value123' and logs to the console
test.data(123) // returns 'value123' but does NOT log to the console
CachedFunction.clearCache(test, 'data', [123]) // clears the cached return value
test.data(123) // returns 'value123' and logs to the console

Property Getters

The cacheGetter() function makes it easy to implement caching on a property getter:

const CachedFunction = require('cached-function')

class TestClass {
  constructor (value) {
    this.value = value
  }

  get data () {
    console.log('The data getter was called')
    return this.value
  }
}

CachedFunction.cacheGetter(TestClass, 'data', {ttl: 5000})

const test = new TestClass('value')
test.data // returns 'value' and logs to the console
test.data // returns 'value' but does NOT log to the console
CachedFunction.clearCache(test, 'data') // clears the cached return value
test.data // returns 'value' and logs to the console

Manual Cache Clearing

The clearCache() function can be used to flush the cache manually.

const CachedFunction = require('cached-function')
let add = CachedFunction((a, b) => a + b)

add(2, 2) // 4
add(2, 2) // returns 4 from the cache

add(5, 5) // 10
add(5, 5) // returns 10 from the cache

// Clears the cached return value for the given arguments:
CachedFunction.clearCache(add, [2, 2])

add(2, 2) // recalculates: 4

add(5, 5) // still cached: 10

Automatic Cache Expiry

Cached return values can be set to expire after a given number of milliseconds. After a value expires, future calls with those arguments will trigger the underlying function once again.

// Each cached return value will have a lifetime of 10 seconds.
func = CachedFunction(func, {ttl: 10000})

Argument Match Modes

By default, the CachedFunction will return a cached return value for a given set of arguments only if those arguments are identical. But if you want, you can disable strict-match mode and can compare arguments by their serialization.

const CachedFunction = require('cached-function')

function callback (stringArg, arrayArg) {
  console.log('Function called')
}

const strict = CachedFunction(callback) // Default behavior

strict('test', [1, 2, 3]) // Logs to the console
strict('test', [1, 2, 3]) // Still logs to the console. The cache is not invoked because it's technically a different array.

const loose = CachedFunction(callback, {strictArgMatch: false})

loose('test', [1, 2, 3]) // Logs to the console
loose('test', [1, 2, 3]) // Doesn't log to the console. Pulls from the cache, because the array's serialization is identical.