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

@avatijs/memoize

v0.1.2

Published

Memoize package part of Avati project

Downloads

147

Readme

Memoize Function Utility

A robust and efficient TypeScript utility for memoizing functions with support for:

  • Cache Size Limits: Control the maximum number of cached entries.
  • Time-to-Live (TTL): Set expiration time for cached entries.
  • LRU Cache Eviction: Automatically evict least recently used items when the cache limit is reached.
  • Complex Argument Handling: Safely memoize functions with complex arguments, including objects with circular references.

Table of Contents


Installation

You can install this utility via npm:

npm install @avatijs/memoize

Usage

Import the memoize function into your project:

import { memoize } from '@avatijs/memoize';

Basic Memoization

Memoize a simple function to cache its results:

function add(a: number, b: number): number {
  console.log('Computing...');
  return a + b;
}

const memoizedAdd = memoize(add);

console.log(memoizedAdd(1, 2)); // Outputs: Computing... 3
console.log(memoizedAdd(1, 2)); // Outputs: 3 (cached result)

Cache Size Limit

Limit the cache size using the maxCacheSize option:

const memoizedAddLimited = memoize(add, { maxCacheSize: 2 });

memoizedAddLimited(1, 2); // Cached
memoizedAddLimited(2, 3); // Cached
memoizedAddLimited(3, 4); // Cached, cache size exceeds limit, least recently used item is evicted

Time-to-Live (TTL)

Set a TTL (in milliseconds) after which cached entries expire:

const memoizedAddWithTTL = memoize(add, { ttl: 5000 }); // Entries expire after 5 seconds

memoizedAddWithTTL(1, 2); // Cached
setTimeout(() => {
  memoizedAddWithTTL(1, 2); // Recomputed after TTL expires
}, 6000);

Handling Complex Arguments

Memoize functions that accept complex arguments, including objects and arrays:

function processData(data: { id: number; value: string }): string {
  console.log('Processing data...');
  return `ID: ${data.id}, Value: ${data.value}`;
}

const memoizedProcessData = memoize(processData);

const data = { id: 1, value: 'Test' };
memoizedProcessData(data); // Outputs: Processing data... 'ID: 1, Value: Test'
memoizedProcessData(data); // Cached result

Preserving this Context

Memoize methods that rely on the this context:

class Multiplier {
  factor = 2;

  multiply = memoize(function (this: Multiplier, x: number) {
    return x * this.factor;
  });
}

const multiplier = new Multiplier();
console.log(multiplier.multiply(5)); // Outputs: 10

Memoizing Asynchronous Functions

Memoize functions that return promises:

async function fetchData(url: string): Promise<string> {
  const response = await fetch(url);
  return response.text();
}

const memoizedFetchData = memoize(fetchData);

memoizedFetchData('https://api.example.com/data').then(console.log);

API

memoize

Creates a memoized version of a function with optional cache size limit and TTL.

Signature

function memoize<Args extends any[], Return>(
  fn: (...args: Args) => Return,
  options?: MemoizeOptions
): (...args: Args) => Return;

Parameters

  • fn: The function to memoize.
  • options (optional): An object specifying memoization options.

MemoizeOptions

An interface defining the memoization options.

Properties

  • maxCacheSize (optional): number
    • Maximum number of entries to store in the cache.
    • Default: Infinity
  • ttl (optional): number
    • Time-to-live in milliseconds for cached entries.
    • Entries expire after ttl milliseconds.
    • Default: undefined (no expiration)

Implementation Details

LRU Cache

An internal Least Recently Used (LRU) cache is used to manage cached entries efficiently. When the cache size exceeds maxCacheSize, the least recently used item is evicted.

Key Generation

A robust key generation function handles complex arguments, including:

  • Primitives: Compared by value.
  • Objects: Compared by identity using a unique ID assigned via a WeakMap.
  • Functions: Stringified to include their code in the key.
  • Circular References: Safely handled without causing errors.

Testing

Extensive test cases have been written using Jest to ensure reliability.

Running Tests

Run the tests:

npx jest

Test Cases Covered

  • Caching Basic Function Calls
  • Handling Different Arguments
  • Max Cache Size Limit
  • Time-to-Live (TTL) Expiration
  • Complex Arguments and Circular References
  • Functions with Side Effects
  • Preservation of this Context
  • Asynchronous Functions
  • Exception Handling
  • Non-Serializable Arguments
  • Zero TTL (No Caching)
  • Multiple Memoized Functions

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

I welcome contributions from developers of all experience levels. If you have an idea, found a bug, or want to improve something, I encourage you to get involved!

How to Contribute

  1. Read Contributing Guide for details on how to get started.
  2. Fork the repository and make your changes.
  3. Submit a pull request, and we’ll review it as soon as possible.

License

MIT License

Avati is open-source and distributed under the MIT License.


Follow on Twitter Follow on LinkedIn Follow on Medium Made with ❤️ Star on GitHub Follow on GitHub