Which Approach to Choose?
Two ways to create objects with methods — each with different trade-offs:
Prototype Methods (Memory-Efficient)
Methods defined ONCE on the prototype, shared by all instances:
function Counter(start) { this.count = start; }
Counter.prototype.increment = function() { this.count++; };
// 1,000 Counters share ONE method in memory ✅
✅ Massive memory savings ⚠️ Methods are public — no true private state
Closure Methods (Private State)
Methods created anew for each object:
function createCounter(start) {
let count = start; // Truly private!
return {
increment() { count++; },
getCount() { return count; }
};
}
// 1,000 counters = 1,000 copies of each method in memory ⚠️
✅ True private variables — strong encapsulation ⚠️ Higher memory usage with many instances
Tip
Use closures for few objects needing strong encapsulation (e.g., a single service). Use prototypes for thousands of instances (e.g., game entities, table rows).
Important
Modern JS engines (V8) optimize prototype lookups extremely well — the speed difference is negligible. Focus on memory and code clarity.