From harness-claude
Implements the Flyweight structural pattern to share intrinsic state across many objects, reducing memory usage when creating thousands+ similar instances in JavaScript.
How this skill is triggered — by the user, by Claude, or both
Slash command
/harness-claude:js-flyweight-patternThe summary Claude sees in its skill listing — used to decide when to auto-load this skill
> Share common state across many fine-grained objects to reduce memory usage
Share common state across many fine-grained objects to reduce memory usage
FlyweightFactory with a Map to return cached instances.class BookFlyweight {
constructor(title, author) {
this.title = title; // intrinsic — shared
this.author = author; // intrinsic — shared
}
}
class BookFactory {
constructor() {
this._books = new Map();
}
getBook(title, author) {
const key = `${title}-${author}`;
if (!this._books.has(key)) {
this._books.set(key, new BookFlyweight(title, author));
}
return this._books.get(key);
}
}
const factory = new BookFactory();
const b1 = factory.getBook('JS Patterns', 'Stoyan');
const b2 = factory.getBook('JS Patterns', 'Stoyan');
console.log(b1 === b2); // true — same instance
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:
When NOT to use:
https://patterns.dev/javascript/flyweight-pattern
npx claudepluginhub intense-visions/harness-engineering --plugin harness-claudeShares fine-grained objects to reduce memory usage by separating intrinsic and extrinsic state. Use when profiling shows many similar objects causing memory or GC pressure.
Use when an application creates a very large number of fine-grained objects whose combined memory cost is prohibitive — share intrinsic state and store extrinsic state externally.
Generates Flyweight pattern for PHP 8.4 to optimize memory usage by sharing intrinsic state across similar objects like icons or glyphs. Includes interface, factory, and unit tests.