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 Pattern4metadata:5 source: https://skillmd.com/skills/intense-visions/js-flyweight-pattern6---7# JS Flyweight Pattern89> Share common state across many fine-grained objects to reduce memory usage1011## When to Use1213- You have a very large number of similar objects (thousands+) and memory usage is a concern14- Objects share most of their state (intrinsic) and only differ in a small extrinsic portion15- Creating new objects per item is prohibitively expensive1617## Instructions18191. Separate **intrinsic state** (shared, immutable) from **extrinsic state** (unique per instance).202. Store intrinsic state in a factory/cache indexed by a key.213. Pass extrinsic state as parameters to methods rather than storing it on the flyweight.224. Use a `FlyweightFactory` with a `Map` to return cached instances.2324```javascript25class BookFlyweight {26 constructor(title, author) {27 this.title = title; // intrinsic — shared28 this.author = author; // intrinsic — shared29 }30}3132class BookFactory {33 constructor() {34 this._books = new Map();35 }3637 getBook(title, author) {38 const key = `${title}-${author}`;39 if (!this._books.has(key)) {40 this._books.set(key, new BookFlyweight(title, author));41 }42 return this._books.get(key);43 }44}4546const factory = new BookFactory();47const b1 = factory.getBook('JS Patterns', 'Stoyan');48const b2 = factory.getBook('JS Patterns', 'Stoyan');49console.log(b1 === b2); // true — same instance50```5152## Details5354The 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.5556**Trade-offs:**5758- Increases code complexity — factory and separation of state59- Thread-safety concerns in worker-based environments if the cache is mutated60- Only beneficial when the number of objects is very large (thousands to millions)6162**When NOT to use:**6364- For typical web UI objects — the browser's memory management handles them fine65- When objects differ significantly from each other — flyweight savings are minimal6667## Source6869https://patterns.dev/javascript/flyweight-pattern7071## Process72731. Read the instructions and examples in this document.742. Apply the patterns to your implementation, adapting to your specific context.753. Verify your implementation against the details and edge cases listed above.7677## Harness Integration7879- **Type:** knowledge — this skill is a reference document, not a procedural workflow.80- **No tools or state** — consumed as context by other skills and agents.8182## Success Criteria8384- The patterns described in this document are applied correctly in the implementation.85- Edge cases and anti-patterns listed in this document are avoided.
Run npx get-skill 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. Independent scanners report: SkillSpector: PASS, Skill Scanner: PASS. Capability flags: docs only. 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.