pattern-flyweight

Share intrinsic state across many objects to reduce memory usage.

Updated Mar 26, 2026
One-click install
npx skills add https://github.com/johnnystefan/test-saas-business --skill pattern-flyweight
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: pattern-flyweight
Source: https://github.com/johnnystefan/test-saas-business/tree/main/skills/design-patterns/structurals/pattern-flyweight
Command: npx skills add https://github.com/johnnystefan/test-saas-business --skill pattern-flyweight

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Large numbers of similar objects each holding entire state waste memory and slow down rendering, caching, or simulation workloads at scale, making the system inefficient.

Core Features & Use Cases

  • State separation: categorize fields into intrinsic (shared, immutable) and extrinsic (contextual) so clients can supply what changes per instance.
  • Centralized flyweight management: reuse a pool of shared objects via a factory that hands out existing instances instead of duplicating data.
  • Use Case: When drawing thousands of trees or caching configuration for large numbers of availability slots, store only one copy of the shared attributes and reuse it across all records while clients manage their unique coordinates or schedules.

Quick Start

Ask the flyweight skill to reuse shared intrinsic state whenever you render large object collections to minimize memory.

Frequently Asked Questions about pattern-flyweight

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I reduce memory usage when rendering thousands of similar objects in TypeScript?

To reduce memory usage for thousands of similar objects, you separate intrinsic shared state from extrinsic contextual state and use a factory-managed pool to reuse shared instances instead of duplicating data. This prevents high memory consumption during large-scale rendering workloads.

What is the flyweight pattern and how does shared state work?

The flyweight pattern is a structural design pattern that minimizes memory by sharing intrinsic immutable state across many object instances. A central factory manages a pool of these shared objects, handing out existing instances to clients rather than creating duplicates.

When should I use a factory-managed pool for high-volume object collections?

Use a factory-managed pool for high-volume object collections when rendering fleets of entities or caching reference data, such as drawing thousands of trees or caching configuration for large numbers of availability slots. It stops the system from becoming inefficient by wasting memory.

How do I separate intrinsic and extrinsic state to optimize memory in TypeScript?

To optimize memory in TypeScript, categorize object fields into intrinsic shared immutable attributes and extrinsic contextual data. Clients supply their unique extrinsic coordinates or schedules, while the factory holds and reuses the single copy of intrinsic shared attributes across all records.

What are the limitations of using shared state for memory optimization?

A limitation of using shared state for memory optimization is the strict requirement to separate intrinsic and extrinsic state, forcing clients to manage all unique contextual data externally. If objects cannot be cleanly divided into shared and contextual fields, the structural pattern will not reduce memory effectively.