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

memofunc

v0.1.6

Published

Automatically memorize your function call

Downloads

1,002

Readme

memofunc

version CI

Automatically memorize your function call. Support any functions in JavaScript, zero or more parameters, primitive or reference parameters, sync or async.

  • Support sync function and async function
  • Use Trie to map parameter and its return value
  • Reference object is diffed shallowly with WeakSet
  • Support custom parameter serializaztion method
  • Support memory and async external cache source at the same time
  • Support fully external cache source

Installation

npm i memofunc

Usage

memoSync

import { memoSync } from 'memofunc'

const addFn = (a: number, b: number) => a + b

const add = memoSync(add)

console.log(add(1, 2))

memoAsync

It also supports memorize async function call. After invoking once for every specified arguments, the result is located in the local memory.

import { memoAsync } from 'memofunc'

function sleep(time: number): Promise<void> {
  return new Promise((res) => {
    setTimeout(() => res(), time);
  });
}

const sort = memoAsync(async (arr: number[]) => {
  // This O(1) sort only run once!
  const res: number[] = [];
  await Promise.all(arr.map(async (a) => {
    await sleep(a * 1000)
    res.push(a)
  }))
  return res;
})

const arr = [3, 1, 2]

console.log(await sort(arr))
console.log(await sort(arr))
console.log(await sort(arr))

It also supports memorize concurrently async function call.

import { memoAsync } from 'memofunc'

let count = 0;
const value = memoAsync(async () => {
  // This function also only run once
  await sleep(100);
  return count++;
});

const task1 = value();
const task2 = value();
const task3 = value();

await Promise.all([task1, task2, task3])

// count === 1

memoExternal

The caching mechanism relies on an external asynchronous service and does not store results in the local memory.

Upon invoking the function, it will:

  1. It retrieves results from the cache. For concurrent function calls, the cache is queried only once like memoAsync;
  2. It either returns the cached results or calls the underlying function if the cache is empty.

This approach facilitates the development of caching solutions within distributed systems, such as Cloudflare Workers.

Consider a scenario where the goal of your proxied function is to query database. Once some distributed nodes update the database, you should invalidate the related cache. This ensures that other nodes, relying entirely on the external cache service, will access the latest data available.

let cnt = 0;
const func = memoExternal(async () => 0, {
  external: {
    async get() {
      await sleep(100);
      return ++cnt;
    },
    async set() {},
    async clear() {
      cnt = 0;
    },
    async remove() {
      cnt = 0;
    }
  }
});

// It will call the external cache get function with cnt = 0
const tasks = await Promise.all([func(), func(), func(), func(), func()]);
expect(tasks).toStrictEqual([1, 1, 1, 1, 1]);

// Clear the cache, cnt = 0
func.clear();

// It will call the external cache get function with cnt = 0
const tasks2 = await Promise.all([func(), func(), func(), func(), func()]);
expect(tasks2).toStrictEqual([1, 1, 1, 1, 1]);

License

MIT License © 2023 XLor