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

memozie

v1.3.5

Published

memoize functions to cache function return values

Downloads

2

Readme

memozie

Dead simple customizable memoization in javascript.

Installation

run npm install memozie

Usage

You can use any number of arguments on functions. Simply wrap the function in memoize function (shown below).

import { memoize, memoizeAll } from "memozie"
const addition = memoize((a,b) => {
  console.log("inside addition function")
  return a + b
})

console.log(addition(4,5)) // outputs "inside addition function" and "9"
addition(4,5) // outputs "9" pulls in value from cached fn results
addition(4,5) // outputs "9" pulls in value from cached fn results

// DEMO WITH ARBITRARY NUMBER OF ARGUMENTS
const mergeObjects = memoize((...objects) => {
  console.log("inside merge objects function")
  return objects.reduce((acc, obj) => Object.assign(acc, obj) , {});
})

console.log(mergeObjects({v: 'v'}, {c : 'c'}, {n: 'n'})) // outputs object and "inside merge objects function"
console.log(mergeObjects({v: 'v'}, {c : 'c'}, {n: 'n'})) // just outputs the object
console.log(mergeObjects({d: 'd'}, {c : 'c'}, {n: 'n'})) // outputs object and "inside merge objects function"


// Can memoize multiple functions and once with memoizeAll

memoizeAll([(a, b) => a-b, (v, c, d) => v - c - d])

Configuration

Second parameter is options which does partial overrides of the defaultOptions (ie you only need to override what you need).

  • getKey(args): Function which generates a key from the arguments to cache value.
  • checkCache(cache, key): Function which checks the cache with the given key to see if value present.
  • run(func, args): Runs passed in function with the given arguments (only occurs when not found in cache).

Examples

// OVERRIDE OF getKey
const additionCustomKey = memoize(
  (a,b) => {console.log("inside addition function"); return a + b;}, 
  {getKey: (args) => args[0] + args[1]}
)

console.log(additionCustomKey(4,5)) // outputs "inside addition function" and "9"
console.log(additionCustomKey(4,5)) // outputs just "9"
console.log(additionCustomKey(3,6)) // outputs just "9" because of custom getKey function

// OVERRIDE OF checkCache
const additionCustomCache = memoize(
  (a,b) => {console.log("inside addition function"); return a + b;}, 
  {checkCache: (cache, key) => key === 0 && key in cache}
)

console.log(additionCustomCache(4,5)) // outputs "inside addition function" and "9"
console.log(additionCustomCache(4,5)) // outputs "inside addition function" and "9" because override of check cache only works for key of zero

// OVERRIDE OF run
const additionCustomRun = memoize(
  (a,b) => {console.log("inside addition function"); return a + b;}, 
  {run: (fn, args) => {console.log("hey"); return fn(...args) }}
)

console.log(additionCustomRun(4,5)) //outputs "hey", "inside addition function" and "9" because of run override
console.log(additionCustomRun(4,5)) //outputs just "9" because hitting cache