- docs/agent/external: Vercel react-best-practices and composition-patterns (MIT), Addy Osmani accessibility (MIT), Anthropic frontend-design (Apache-2.0), official Payload skill at v3.92.1 (MIT) — text only, each with its licence, SOURCE.md (repo, commit) and an override note in front of the SKILL.md (ipal-kit first, Payload fetching over swr, DESIGN.md tokens, Payload 3 not 4); manifest.json with sha256 per file - scripts/vendor-external-skills.mjs (maintainer, not shipped): fetches by full commit SHA only, rejects non-text or executable files, regenerates the manifest; --check verifies it - ipal-skills --external [names]: copies ext-* after checking every file against the manifest, downloads nothing, never touches ext-* folders without ipal-kit's SOURCE.md, removes only retired ones it installed; an unknown name stops the run before anything is copied - AGENT.md §14, ipal-setup stage 2 (optional step), README: usage, overrides, how to update the pins - test/externalSkills.test.ts: pins are SHAs, no branch fetch, checksums, text only, licences, override note, installer leaves other folders alone Co-Authored-By: Claude Opus 5.5 <[email protected]>
81 lines
1.9 KiB
Markdown
81 lines
1.9 KiB
Markdown
---
|
|
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)
|