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

memora

v1.0.1

Published

Memora: A lightweight, TTL-based JavaScript caching library. It efficiently memoizes function outputs, supporting multiple storage options like memory and localStorage for optimized performance.

Downloads

8

Readme

Memora

Memora is a highly efficient, customizable JavaScript caching library designed for effortless memoization of function outputs. With its advanced features like Time-To-Live (TTL), various storage options, and a sophisticated hashing mechanism, Memora optimizes performance in web applications by intelligently caching the results of function calls.

Features

  • Time-To-Live (TTL): Automatically expires cached data.
  • Multiple Storage Options: Supports LocalStorage and can be extended to other storage mechanisms.
  • Function and Argument Tracking: Recomputes cache when either the function implementation or arguments change.
  • Customizable Hash Function: Allows for a tailored hashing mechanism.
  • Async Function Support: Seamlessly handles asynchronous functions.

Installation

Install Memora via npm, yarn or pnpm

npm install memora
yarn add memora
pnpm add memora

Basic Usage

Importing

import { Memora } from 'memora';

Simple Memoization

const memora = new Memora();
const square = (n) => n * n;
const memoizedSquare = memora.memoize(square);

console.log(memoizedSquare(4));  // Computed
console.log(memoizedSquare(4));  // Cached

Advanced Usage

Memoizing Async Functions

const fetchData = async (id) => { /* ... */ };
const memoizedFetchData = memora.memoizeAsync(fetchData);

memoizedFetchData('abc').then(console.log);  // Computed
memoizedFetchData('abc').then(console.log);  // Cached

Using Custom Hash Function

const customHashFunction = (input) => { /* ... */ };
const customMemora = new Memora();
customMemora.setHasher(customHashFunction);

Custom Key Generator

const customKeyGenerator = (func, ...args) => 'custom_' + JSON.stringify(args);
customMemora.setKeyGenerator(customKeyGenerator);

API Documentation

  • Memora(options): Constructor to create a new Memora instance.

    • options: Object (optional)
      • name: String (optional) - Unique name for the function caching (if not provided function content will be used).
      • prefix: String (optional) - Prefix for cache keys.
      • Storage: StorageProvider (optional) - Storage provider for caching, default localStorage
      • TTL: Number (milliseconds) - Time to live for cached data in milliseconds, default 1 day.
  • memoize(func): Memoizes a given function.

    • func: Function - The function to be memoized.
  • memoizeAsync(asyncFunc): Memoizes an asynchronous function.

    • asyncFunc: Async Function - The async function to be memoized.
  • setKeyGenerator(func): Sets a custom key generator function.

    • func: Function - Custom key generator.
  • setHasher(func): Sets a custom hash function for generating keys.

    • func: Function - Custom hash function.

Contributing

Contributions to Memora are welcome! Please follow the coding standards and include tests for new features.

License

Memora is MIT licensed.