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

memoizeit

v3.1.1

Published

Module for functions memoization.

Downloads

418

Readme

MemoizeIt

npm version Build Status js-standard-style Known Vulnerabilities

 

MemoizeIt is a powerful JavaScript package that allows you to improve the performance of expensive functions. By "memorizing" the output of a function for specific input values. MemoizeIt is able to return the memorized output immediately when called with the same arguments, instead of having to calculate the result again.

 

Memoisation is an optimization technique used primarily to speed up computer programs by storing the results of expensive function calls and returning the cached result when the same inputs occur again.

More about memoization here: https://en.wikipedia.org/wiki/Memoization


 

Note: This module should only be used with pure functions

A pure function is a function that has the following properties:

  1. the function return values are identical for identical arguments (no variation with local static variables, non-local variables, mutable reference arguments or input streams), and
  2. the function has no side effects (no mutation of local static variables, non-local variables, mutable reference arguments or input/output streams).

More about pure functions here: https://en.wikipedia.org/wiki/Pure_function

 

Installation

To use MemoizeIt, simply install it via npm:

npm i memoizeit

  ... or via yarn:

yarn add memoizeit

 

Usage

Then import it into your code and use it:

import { memoizeit } from 'memoizeit'

  ... or with require (CommonJS):

const memoizeit = require('memoizeit')

 

To benefit from MemoizeIt performance improvements, simply pass the function to be memoized as an input parameter:

// function to be memoized
function sum (a, b) {
  return a + b
}

// memoization of the sum function
const memoSum = memoizeit(sum)

 

Memoized functions can then be called as normal, with the added bonus of improved performance for repeated calls with the same arguments:

// using the memoized function
const result1 = memoSum(1, 2) // first call (no cached value)
console.log(result1) // prints 3

const result2 = memoSum(1, 2) // second call (cache usage)
console.log(result2) // prints 3

 

Promises are supported

Functions that return promises are supported.

import memoizeit from 'memoizeit'

// promise function to be memoized
async function promiseIncrementOne (ms, value) {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve(Number(value) + 1)
    }, ms)
  })
}

async function init () {
  // memoization of the promised function
  const memoizedPromise = memoizeit(promiseIncrementOne)

  // using the memoized function
  console.time('First time')
  console.log('First call calculation:', await memoizedPromise(3000, 4)) // first call (no cached value)
  console.timeEnd('First time') // First time: 3.012s
  console.log()
  console.log(' #################### ')
  console.log()
  console.time('second time')
  console.log('Second call calculation:', await memoizedPromise(3000, 4)) // second call (cache usage)
  console.timeEnd('second time') // second time: 0.078ms
}

init()

 

Functions that return objects (including arrays)

The memorized functions of functions that return objects (including arrays) return a reference to the object. If the object is changed, the next calls to the memorized function will return a reference to the modified object.

import memoizeit from 'memoizeit'

function getObject (arg1, arg2) {
  return { a: arg1, b: arg2 }
}

const memoGetObject = memoizeit(getObject)
const resultObject = memoGetObject('x', 'y')
console.log(resultObject) // { a: 'x', b: 'y' }

resultObject.c = 'z'
const resultObject2 = memoGetObject('x', 'y')
console.log(resultObject) // { a: 'x', b: 'y', c: 'z' }

 

Some examples

Fibonacci

The calculation of a high number in the fibonacci sequence requires a lot of computation as it is necessary to recursively calculate the fibonacci value of smaller numbers.

Image

As we see in the image to calculate the fibonacci value for the number 7, we need to calculate the fibonacci value several times from other lower numbers. The fibonacci for the number 2 is calculated 8 times. By using memoization we only have to do the calculation once and save the result to be used immediately in the next calculation, saving a lot of time and computing resources.

 

Here's an example of how to use MemoizeIt to improve the performance of an expensive function that calculates Fibonacci numbers recursively:

import memoizeit from 'memoizeit'

// fibonacci function to be memoized
function fibonacciWithMemoizeIt (n) {
  if (n <= 1) return n
  return memoizedFibonacci(n - 1) + memoizedFibonacci(n - 2)
}

// fibonacci non-memoized function
function fibonacciWithoutMemoizeIt (n) {
  if (n <= 1) return n
  return fibonacciWithoutMemoizeIt(n - 1) + fibonacciWithoutMemoizeIt(n - 2)
}

// memoization of the fibonacci function
const memoizedFibonacci = memoizeit(fibonacciWithMemoizeIt)

// using the memoized function
console.time('memoizedit time')
console.log('Memoized calculation:', memoizedFibonacci(45)) // Memoized calculation: 1134903170
console.timeEnd('memoizedit time') // memoizedit time: 7.275ms

// use of the non-memoized function
console.time('not memoizedit time')
console.log('Not memoized calculation:', fibonacciWithoutMemoizeIt(45)) // Not memoized calculation: 1134903170
console.timeEnd('not memoizedit time') // not memoizedit time: 17.834s

 

Authors

 

License

MIT