Course contentsShow
← JavaScript
Lesson 567 of 2,72314. The 'this' KeywordFree lesson

Performance Considerations

The cost of creating bound functions and when to consider alternatives like closures.

Performance Considerations

You'll learn: When creating bound functions costs too much and when simpler alternatives like closures are faster.

The Cost of bind()

Every time you call bind(), JavaScript creates a new function object in memory. This isn't free—it takes time and space. If you're binding inside a loop, or on every render in a UI framework, or thousands of times in quick succession, those allocations add up.

Think of bind() like ordering a custom sandwich: you get exactly what you want, but it takes a moment to prepare. If you need one sandwich, great. If you need a thousand, maybe just grab pre-made ones off the shelf.

When bind() Hurts Performance

// BAD: Creates 1000 new functions
for (let i = 0; i < 1000; i++) {
  setTimeout(function() {
    console.log(this.value);
  }.bind(obj), i * 10);
}

Each iteration creates a fresh bound function—1000 allocations!

The Closure Alternative

// BETTER: Creates 1000 closures (lighter weight)
for (let i = 0; i < 1000; i++) {
  const value = obj.value; // capture in lexical scope
  setTimeout(function() {
    console.log(value);
  }, i * 10);
}

Closures capture variables directly without creating a binding wrapper. They're typically faster and use less memory.

When bind() Is Fine

Don't avoid bind() obsessively—for one-time setups (event handlers, React component methods), the cost is negligible. Profile before optimizing.

Key Takeaway: bind() creates new function objects with overhead; for repeated or high-frequency scenarios, closures capturing specific values are often more performant than binding entire contexts.