1

What is more efficient in jquery:

if ($.isFunction(func_name)
    $.apply(func_name, [param, etc])

or

if ($.isFunction(func_name)
    func_name(param, etc)

What are the advantages to call $.apply() or $.call() over calling directly the object if it is a function as a callback.

Thanks

Miawa
  • 282
  • 1
  • 4
  • 10
  • 1
    I think he wants to know why you would use apply or call, not which is fastest. Obviously the native javascript function call is the fastest. – Reinstate Monica Cellio Jun 03 '13 at 14:11
  • 5
    $.apply and $.call are *not* jQuery methods - they are Function.prototype.apply and Function.prototype.call. – clinton3141 Jun 03 '13 at 14:16
  • 2
    I've never heard of `$.apply`. I assume it calls the native `apply()`, which is different than calling a function "normally". If you want to set `this` inside the function, use `apply`/`call`, if you don't, then call the function normally `func_name()`. Also, use `apply` if you have an array of params instead of knowing them ahead of time. They have different purposes, so neither is more "efficient". – gen_Eric Jun 03 '13 at 14:16
  • 1
    Care to rephrase the question and maybe revisit your code samples - maybe for compile errors? And whether they actually show what you are asking? – dualed Jun 03 '13 at 14:18
  • Assuming that `func_name` is a function, they're not even equivalent but do different things - you can't compare efficiency. – Bergi Jun 03 '13 at 14:25
  • Kind of a duplicate of http://stackoverflow.com/q/15903782/218196. – Felix Kling Jun 03 '13 at 14:26
  • Yes @Archer , I thought it would be faster using native JavaScript, but as someone suggested i've created [this test](http://jsperf.com/apply-vs-function) in [jsperf](http://jsperf.com) which is giving me a different result as I expected. – Miawa Jun 03 '13 at 14:44
  • @Rui: It was already said, but your test is flawed. `$.apply(bar, ["hello"]);` is something **totally different** than `bar.apply(["hello"])`. In the first case you are calling the `$` function with `this` being set to `bar` and passing `"hello"` as first argument. In the second case you are calling the `bar` function with `this` being set to the array `["hello"]` and passing no arguments. Since `$` and `bar` are two different functions, there will of course be a performance difference. – Felix Kling Jun 03 '13 at 19:37

1 Answers1

3

Unless you're manipulating contexts, there's no reason to use call or apply or the equivalent jQuery helpers.

Also, you don't need to use jQuery to test if a function exists.

function foo() { alert('foo') }

if (foo && typeof foo === 'function') {
    foo();
}

Fiddle

pdoherty926
  • 9,895
  • 4
  • 37
  • 68
  • 2
    +1 As a matter of fact, [you don't *need* jQuery for anything](http://vanilla-js.com/). – Niet the Dark Absol Jun 03 '13 at 14:24
  • Visit http://vanilla-js.com/ for the best framework ever ☺ Still `$.isfunction()` is shorter and easier too read. – Christoph Jun 03 '13 at 14:25
  • Yes, this is the answer i expected, no jQuery at all = faster. Here is a [test](http://jsperf.com/apply-vs-function) i made, it looks like the jQuery.apply() is faster. – Miawa Jun 03 '13 at 14:57
  • 1
    While this is highly subjective, I'm much more concerned with readability and maintainability than performance. I'd argue that `if ($.isFunction(bar)) $.apply(bar, ["hello"]);` will cause noobs to do a double-take more often than, `if (foo && typeof foo === 'function') { foo('hello'); }`. (Even more so for the equivalent CoffeeScript, `foo('hello') if foo and typeof foo is 'function'`.) – pdoherty926 Jun 03 '13 at 15:13