hot-memo
v1.1.1
Published
A lightweight memoization utility designed specifically for Bun's hot reload mode. This package helps preserve expensive computation results across hot reloads during development, significantly improving developer experience.
Downloads
5,304
Readme
hot-memo
A lightweight memoization utility designed specifically for Bun's hot reload mode. This package helps preserve expensive computation results across hot reloads during development, significantly improving developer experience.
Why hot-memo?
When using Bun's hot reload feature (bun --hot), your application restarts whenever files change. Without memoization, expensive operations like API calls, file processing, or complex calculations are repeated on every reload, slowing down development.
hot-memo solves this by storing computation results in global memory that persists across hot reloads, using a unique salt that changes only when the process truly restarts.
Installation
bun add hot-memoUsage
import { hotMemo } from "hot-memo";
// Expensive operation that you don't want to repeat on every hot reload
const fetchUserData = async (userId: string) => {
console.log("Fetching user data..."); // This will only log once per process
const response = await fetch(`/api/users/${userId}`);
return response.json();
};
// Memoized version - result persists across hot reloads
const userData = await hotMemo(fetchUserData, ["user123"]);How it works
- Creates a unique salt when the process starts
- Uses function signature + arguments + salt as the cache key
- Stores results in
globalThisto survive hot reloads (will switch to import.meta.hot in future if it's stable enough.) - Automatically handles async functions and returns awaited results
API
hotMemo<Args, R>(fn, args, key?)
fn: The function to memoizeargs: Arguments to pass to the function (as readonly array)key: Optional custom cache key (auto-generated if not provided)
Returns a Promise that resolves to the memoized result.
Development
# Install dependencies
bun install
# Build
bun run build
# Format code
bun run fmtLicense
MIT
