The Flyweight pattern is a structural memory optimization. It trades CPU time (factory lookup) for memory savings by sharing object instances. JavaScript's garbage collector normally handles short-lived objects efficiently, so Flyweight is only necessary at extreme scale.
Trade-offs:
Increases code complexity — factory and separation of state
Thread-safety concerns in worker-based environments if the cache is mutated
Only beneficial when the number of objects is very large (thousands to millions)
When NOT to use:
For typical web UI objects — the browser's memory management handles them fine
When objects differ significantly from each other — flyweight savings are minimal
Read the instructions and examples in this document.
Apply the patterns to your implementation, adapting to your specific context.
Verify your implementation against the details and edge cases listed above.
Harness Integration
Type: knowledge — this skill is a reference document, not a procedural workflow.
No tools or state — consumed as context by other skills and agents.
Success Criteria
The patterns described in this document are applied correctly in the implementation.
Edge cases and anti-patterns listed in this document are avoided.
1---2name: js-flyweight-pattern3description: JS Flyweight Pattern4---5# JS Flyweight Pattern67> Share common state across many fine-grained objects to reduce memory usage89## When to Use1011- You have a very large number of similar objects (thousands+) and memory usage is a concern12- Objects share most of their state (intrinsic) and only differ in a small extrinsic portion13- Creating new objects per item is prohibitively expensive1415## Instructions16171. Separate **intrinsic state** (shared, immutable) from **extrinsic state** (unique per instance).182. Store intrinsic state in a factory/cache indexed by a key.193. Pass extrinsic state as parameters to methods rather than storing it on the flyweight.204. Use a `FlyweightFactory` with a `Map` to return cached instances.2122```javascript23class BookFlyweight {24 constructor(title, author) {25 this.title = title; // intrinsic — shared26 this.author = author; // intrinsic — shared27 }28}2930class BookFactory {31 constructor() {32 this._books = new Map();33 }3435 getBook(title, author) {36 const key = `${title}-${author}`;37 if (!this._books.has(key)) {38 this._books.set(key, new BookFlyweight(title, author));39 }40 return this._books.get(key);41 }42}4344const factory = new BookFactory();45const b1 = factory.getBook('JS Patterns', 'Stoyan');46const b2 = factory.getBook('JS Patterns', 'Stoyan');47console.log(b1 === b2); // true — same instance48```4950## Details5152The Flyweight pattern is a structural memory optimization. It trades CPU time (factory lookup) for memory savings by sharing object instances. JavaScript's garbage collector normally handles short-lived objects efficiently, so Flyweight is only necessary at extreme scale.5354**Trade-offs:**5556- Increases code complexity — factory and separation of state57- Thread-safety concerns in worker-based environments if the cache is mutated58- Only beneficial when the number of objects is very large (thousands to millions)5960**When NOT to use:**6162- For typical web UI objects — the browser's memory management handles them fine63- When objects differ significantly from each other — flyweight savings are minimal6465## Source6667https://patterns.dev/javascript/flyweight-pattern6869## Process70711. Read the instructions and examples in this document.722. Apply the patterns to your implementation, adapting to your specific context.733. Verify your implementation against the details and edge cases listed above.7475## Harness Integration7677- **Type:** knowledge — this skill is a reference document, not a procedural workflow.78- **No tools or state** — consumed as context by other skills and agents.7980## Success Criteria8182- The patterns described in this document are applied correctly in the implementation.83- Edge cases and anti-patterns listed in this document are avoided.
Run npx skillmds@latest add intense-visions/js-flyweight-pattern in your terminal (requires Node.js), paste this page's agent-chat prompt into Claude, Cursor, or any MCP-connected agent, or download the SKILL.md file and copy it into your agent's skills directory.
JS Flyweight Pattern It is listed under Coding & Dev Tools on SkillMD.
This skill has not completed SkillMD's automated safety review yet. SkillMD never runs a skill's scripts for you; review the SKILL.md before installing.
This skill is tagged as working with Claude Code, Claude.ai, OpenAI Codex. SKILL.md is an open format, so most agents that read a skills directory can load it too.
Yes. Installing skills from SkillMD is free, and the skill stays under its author's original license.
Intense-Visions (@intense-visions) published this skill. Their other Agent Skills are listed on their SkillMD profile.