Flyweight Pattern

Teaches the flyweight pattern for memory optimization. Use when your application creates large numbers of similar objects and memory consumption is a concern.

259 stars 28 forksSoftware Development

Install

skills CLI (Claude Code, Cursor, Codex and more)
npx skills add PatternsDev/skills --skill flyweight-pattern
Manual install
git clone --depth 1 https://github.com/PatternsDev/skills.git /tmp/skills
mkdir -p ~/.claude/skills
cp -r /tmp/skills/javascript/flyweight-pattern ~/.claude/skills/flyweight-pattern

Personal skills live in ~/.claude/skills. Use .claude/skills in a project folder to share a skill with your team.

From the SKILL.md

# Flyweight Pattern

The flyweight pattern is a useful way to conserve memory when we're creating a large number of similar objects.

In our application, we want users to be able to add books. All books have a `title`, an `author`, and an `isbn` number! However, a library usually doesn't have just one copy of a book: it usually has multiple copies of the same book.

## When to Use

- Use this when creating a huge number of objects that could potentially drain available memory
- This is helpful when many objects share the same intrinsic properties (e.g., books with the same ISBN)

## When NOT to Use

- When the number of objects is small enough that memory is not a concern
- When objects have few or no shared intrinsic properties — the separation of intrinsic and extrinsic state adds complexity without savings
- When the added lookup/management overhead outweighs the memory benefit

## Instructions

- Separate intrinsic (shared) state from extrinsic (unique) state
- Use a Map or similar structure to cache and reuse shared object instances
- Consider JavaScript's prototypal inheritance as a simpler alternative in many cases

## Details

It wouldn't be very useful to create a new book instance each time if there are multiple copies of the exact same book. Instead, we want to create multiple instances of the `Book` constructor, that represent a single book.
Read the full skill on GitHub

Details

Skill name
flyweight-pattern
Path
javascript/flyweight-pattern/SKILL.md
Repo updated
15 April 2026

FAQ

What does the Flyweight Pattern skill do?

Teaches the flyweight pattern for memory optimization. Use when your application creates large numbers of similar objects and memory consumption is a concern.

How do I install the Flyweight Pattern skill?

skills CLI (Claude Code, Cursor, Codex and more): npx skills add PatternsDev/skills --skill flyweight-pattern. Skills go in ~/.claude/skills (personal) or .claude/skills (project), and Claude loads them automatically when a task matches the description.

Is Flyweight Pattern popular?

It lives in PatternsDev/skills, which has 259 GitHub stars and 28 forks.

Related skills

Browse the most popular Claude skills, all software development skills, or write your own with the skills generator.