js-cache-function-results — Claude Code rule: how to install
js-cache-function-results instructs the agent to cache repeated function calls using a module-level Map. It eliminates redundant computations across renders and utility calls.
MIT ★ 7,191 updated 2026-10-24
.claude/rules/js-cache-function-results.mdWhat js-cache-function-results does
The rule directs Claude Code to avoid redundant computation by caching repetitive function calls with a module-level Map. If an operation runs repeatedly on identical inputs during rendering, the cached result is returned immediately. Redundant work stops.
For single-value checks like reading an authentication cookie, it recommends a module-scoped variable that gets reset when state changes. Unlike React hooks, this approach functions across utility files, event handlers, and plain scripts.
Because cache entries persist in module memory indefinitely, stale data must be invalidated manually when underlying sources change.
What it helps with
- stop repeated computations during array rendering in UI components
- cache expensive utility outputs outside React hook lifecycles
- memoize repeated cookie lookups with module-scoped state
- invalidate cached values manually on authentication changes
Example prompts for the agent
- Optimize repetitive
slugifycalls inProjectList.tsxusing a module-levelMap - Review the list rendering logic and cache identical function inputs at the module scope
- Add a cached
isLoggedIncheck using a module variable that resets on auth change
How to install js-cache-function-results
Manually: put the files in place
.claude/rules/js-cache-function-results.mdThe paths frontmatter field limits the files
AGENTS.md
Codex reads AGENTS.md: paste the rule text there
Manually: put the files in place
.cursor/rules/js-cache-function-results.mdcFrontmatter: description, globs, alwaysApply
Manually: put the files in place
.github/instructions/js-cache-function-results.instructions.mdThe applyTo field sets which files it applies to
Manually: put the files in place
.devin/rules/js-cache-function-results.mdManually: put the files in place
.clinerules/js-cache-function-results.mdStep by step, with commands for each platform: AGENTS.md vs CLAUDE.md: what they are and which one to use
Questions about js-cache-function-results
How do I install js-cache-function-results in Claude Code?
Save the rule content into .claude/rules/js-cache-function-results.md in your project root.
What does js-cache-function-results do?
It prompts the coding agent to memoize repeated function calls with a module-level Map during rendering loops.
Why prefer a Map over React hooks for caching?
A module-scoped Map operates across event handlers, background utilities, and regular functions without being restricted to React component lifecycles.
Source text
js-cache-function-results.md · MIT
---
title: Cache Repeated Function Calls
impact: MEDIUM
impactDescription: avoid redundant computation
tags: javascript, cache, memoization, performance
---
## Cache Repeated Function Calls
Use a module-level Map to cache function results when the same function is called repeatedly with the same inputs during render.
**Incorrect (redundant computation):**
```typescript
function ProjectList({ projects }: { projects: Project[] }) {
return (
<div>
{projects.map(project => {
// slugify() called 100+ times for same project names
const slug = slugify(project.name)
return <ProjectCard key={project.id} slug={slug} />
})}
</div>
)
}
```
**Correct (cached results):**
```typescript
// Module-level cache
const slugifyCache = new Map<string, string>()
function cachedSlugify(text: string): string {
if (slugifyCache.has(text)) {
return slugifyCache.get(text)!
}
const result = slugify(text)
slugifyCache.set(text, result)
return result
}
function ProjectList({ projects }: { projects: Project[] }) {
return (
<div>
{projects.map(project => {
// Computed only once per unique project name
const slug = cachedSlugify(project.name)
return <ProjectCard key={project.id} slug={slug} />
})}
</div>
)
}
```
**Simpler pattern for single-value functions:**
```typescript
let isLoggedInCache: boolean | null = null
function isLoggedIn(): boolean {
if (isLoggedInCache !== null) {
return isLoggedInCache
}
isLoggedInCache = document.cookie.includes('auth=')
return isLoggedInCache
}
// Clear cache when auth changes
function onAuthChange() {
isLoggedInCache = null
}
```
Use a Map (not a hook) so it works everywhere: utilities, event handlers, not just React components.
Reference: [How we made the Vercel Dashboard twice as fast](https://vercel.com/blog/how-we-made-the-vercel-dashboard-twice-as-fast)
© tech-leads-club · MIT · tech-leads-club/agent-skills