Flyweight in JavaScript
The flyweight pattern is where the subset of object properties that have the same value are stored in shared “flyweight” objects.
The flyweight pattern is useful when generating large quantities of objects that share a subset of the same values.
Implementation
One concept from domain-driven design by Eric Evans is “value objects”. These value objects have the property that their contents matter more than their identity. Let’s take the example of a value object being a “coin” where, for the purposes of payment, two 50-cent coins are interchangeable.
Value objects are interchangeable and immutable (a 50-cent coin can’t become a 10-cent coin). These types of objects are therefore a great fit for the Flyweight pattern.
Not all properties of a “coin” are “value”-driven, for example, certain coins are made from certain materials and coins tend to be issued in a certain...