"JS Basics" Why is call faster than apply?

Source: Internet
Author: User

This is a very interesting question.

In the process of looking at the source code, will always encounter such a writing:

1 varTriggerevents =function(events, args) {2     varEV, i =-1, L = events.length, a1 = Args[0], a2 = args[1], a3 = args[2];3     Switch(args.length) {4        Case0: while(++i < L) (ev = Events[i]). Callback.call (EV.CTX);return;5        Case1: while(++i < L) (ev = Events[i]). Callback.call (Ev.ctx, A1);return;6        Case2: while(++i < L) (ev = Events[i]). Callback.call (Ev.ctx, a1, A2);return;7        Case3: while(++i < L) (ev = Events[i]). Callback.call (Ev.ctx, A1, A2, A3);return;8       default: while(++i < L) (ev = Events[i]). callback.apply (Ev.ctx, args);return;9     }Ten};

(Code from Backbone)

The author takes precedence over the call method for handling events when the parameter is within 3 (including 3). Consider using the Apply method when there are too many parameters (3 extra).   The reason for this is that call is faster than apply. There are a lot of examples on the internet to prove that call is faster than apply. You can look at the performance comparison of the call and apply in this article, the example is very comprehensive. Or you can write a few simple, test it yourself.  Here to recommend a magical website Jsperf, for testing JS performance. A few simple examples:

Why is call faster than apply? Here's what happens when they are called.

Function.prototype.apply (Thisarg, Argarray)

1, if iscallable (function) is false, that is, function can not be called, then throw a TypeError exception.
2, if Argarray is null or undefined, returns the result of the [[call]] Internal method calling function, providing Thisarg and an empty array as arguments.
3. If Type (Argarray) is not an object, the TypeError exception is thrown.
4, get the length of Argarray. Call Argarray's [[Get]] internal method to find the property length. Assign a value to Len.
5. Define n as ToUint32 (len). ToUint32 (len) method: Converts its parameter len to one of the 2^32 integer values ranging from 0 to 2^32-1.
6. Initialize argList as an empty list.
7. Initialize index to 0.
8, the loop iteration takes out the Argarray. Repeating loop while (Index < n)
A. Convert the subscript to a string type. Initializes the IndexName to ToString (index).
B. Define NEXTARG as the result of a [[Get]] internal method that calls Argarray using IndexName as a parameter.
C, add Nextarg to argList as the last element.
D, SET index = index+1
9. Returns the result of the [[call]] internal method calling Func, providing Thisarg as the value, arglist as the parameter list.

Function.prototype.call (Thisarg [, Arg1 [, arg2, ...])

1, if iscallable (function) is false, that is, function can not be called, then throw a TypeError exception.
2. Define arglist as an empty list.
3. If you call this method with more than one parameter, append each parameter to the last element of arglist in order from left to right starting with arg1
4. Returns the result of the [[call]] internal method calling Func, providing Thisarg as the value, arglist as the parameter list.


We can see that the obvious apply is much more than the step of call.
Because of the parameter format (array) defined in apply, more needs to be done after being called, and the given parameter format needs to be changed (step 8). There are also some parameter checks (step 2) that are not necessary in call.
Another important point: in Apply no matter how many parameters, will be executed loop, that is, step 6-8, in call is corresponding to step 3, it is necessary to be executed.

In summary, the call method is faster than apply because the parameter format of the calls method is exactly the format required by the internal method.

Reference and study: http://es5.github.io/#x15.3.4.3https://stackoverflow.com/questions/23769556/why-is-call-so-much-faster-than-apply

"JS Basics" Why is call faster than apply?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.