jQuery bind() unbind() and on() and off()

bind, events, javascript, jquery, performance

Solution

Unbinding only to bind again for performance reasons is probably bug-prone and makes things overly complicated in most cases.

Instead of binding event listeners on many specific DOM elements, you could take a more "birds eye" approach and bind just a few listeners near the top of the DOM tree, and then when the event is triggered check what was actually clicked.

That way you won't spend CPU on binding/unbinding lots of event listeners, but instead take a small CPU hit when an event is processed (which is usually not noticeable).

This is covered in detail here: event delegation vs direct binding when adding complex elements to a page

Problem

Im working on a small adminarea for a webpage. Does it make sense to unbind events for increasing performance(client)? Or does it cost more performance to unbind events and binding it 30Seconds later again? My questions: Is the idea behind bind()-unbind() or on().off() just increasing clientbased performance or should i use it for other scenarios? This question comes because my javascript code is growing and growing (about 30%) because of unbinding events. And i think, that some things may not work, when user interacts not, as i want... . EDIT: The most times im binding/unbinding keypress events, because i need the arrow keys for diff. scenarios.

Original source

Related problems