4

I know the difference between delegate() and on(), but I have a doubt that on() is very popular and faster than delegate() then why jquery is not removing the delegate() from its library.

live() has drawbacks then it is removed from jQuery-1.9 version. Also if we see the performance between on() and delegate() then on() is much faster than delegate().

Then any reason to keep delegate() in jQuery?

Community
  • 1
  • 1
Rohan Kumar
  • 40,431
  • 11
  • 76
  • 106

5 Answers5

3

From the documentation:

As of jQuery 1.7, .delegate() has been superseded by the .on() method.

It is just legacy code. Don't use it.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • Agreed, and saw the code `delegate: function( selector, types, data, fn ) { return this.on( types, selector, data, fn );},` in `jquery 1.9`, so this should be removed from jquery if their is no difference, not? Same question may raise for `bind()`. – Rohan Kumar Apr 07 '14 at 06:29
  • "Should be removed" is a somewhat subjective question of backwards-compatibility vs streamlining of the API. – Quentin Apr 07 '14 at 06:30
3

This is what a member of the jQuery team wrote in regard to this issue in 2013:

"We haven't deprecated .bind or .delegate, nor have we indicated we're removing them at any point.

They're each only one line in the source so size reduction isn't much of a reason to remove them.

We deprecated .live because it has lots of confusing issues that we can't fix."

Basically, while delegate() has been superseded by on(), there are no future plans to deprecate delegate() due to the fact it takes up very little space (and doesn't cause any problems).

dsgriffin
  • 66,495
  • 17
  • 137
  • 137
  • "It takes up very little space" see the `live()` on http://code.jquery.com/jquery-1.7.1.js I don't think it also takes too much space then delegate() – Rohan Kumar Apr 07 '14 at 06:38
  • @RohanKumar True, but as mentioned live() was removed for other reasons – dsgriffin Apr 07 '14 at 06:42
2

I think it is more to do with whether they want to break the users who has used it.... in terms of delegated handlers I don't see any performance diff between them...

So as advised new code should use .on() not .delegate

Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
  • So, somehow you agreed with removing delegate() from jquery, and you can check the performance on http://jsperf.com/jquery-event-delegation/85 – Rohan Kumar Apr 07 '14 at 06:34
  • @RohanKumar the performance difference is because `.on()` has to do some conditional checks to see what kind of event has to be registered – Arun P Johny Apr 07 '14 at 06:55
2

on is meant to replace delegate. Probably the jquery authors might keep it sometime long to make it more backward compatible. Many old plugins still use those methods. probably that might be the reason to keep it.

Alok Swain
  • 6,409
  • 5
  • 36
  • 57
2

You can perform both event binding and delegation with the help of on() method. Just the syntax differs.

Just visit this link:

http://learn.jquery.com/events/handling-events/

They have beautifully explained with examples

Abhinav
  • 8,028
  • 12
  • 48
  • 89