BrianFreud
BrianFreud

Reputation: 7464

jQuery: For events bound with .on(), which is better/faster: expressly removing events, or letting remove() handle it?

In jQuery, for events bound with .on(), which is better/faster: expressly removing events, or letting remove() handle it?

Letting remove() handle it is easier, but is there a performance hit (and if so, how much of one?), or is it better to keep track of bound events and expressly unbind them prior to doing the remove()?

Upvotes: 1

Views: 73

Answers (3)

Eric Hodonsky
Eric Hodonsky

Reputation: 5897

use .off();

You can use it as unbind/undelegate etc etc....

$("#item").off("click", "a");

Upvotes: 0

Kevin B
Kevin B

Reputation: 95065

If you were to remove them manually, you would have to remove the events, their data, and all data that jQuery uses on the element as well as any data that any plugins may be storing on the element. It would be much safer/easier and more than likely faster to simply use .remove()

Upvotes: 0

TimWolla
TimWolla

Reputation: 32711

I assume the jQuery developers know best which events are bound and how to effectively remove them: Let jQuery handle it in remove(). I don't know any script that manually unbinds the events because of performance reasons (Which are probably marginal)

Upvotes: 2

Related Questions