Discussing Function. apply () II ------ using Apply parameter arrays to improve JavaScript program performance _ javascript skills

Source: Internet
Author: User
Discuss Function. apply () 2 ------ use Apply parameter arrays to improve JavaScript program performance. Let's talk about Function. apply () in improving program performance.

Let's start with the Math. max () function. Math. max can be followed by any parameter and return the maximum value among all parameters.

For example
Alert (Math. max (5, 8) // 8
Alert (Math. max (,) // 9

However, in many cases, we need to find the largest element in the array.
Var arr = [5, 7, 9, 1]
Alert (Math. max (arr) // This is not acceptable. Be sure to write it like this

Function getMax (arr ){
Var arrLen = arr. length;
For (var I = 0, ret = arr [0]; I ret = Math. max (ret, arr [I]);
}
Return ret;
}
This is troublesome and inefficient. If you use apply, check the Code:
Function getMax2 (arr ){
Return Math. max. apply (null, arr)
}
The two pieces of code achieve the same purpose, but getMax2 is elegant, efficient, and concise.

Performance test:
GetMax performance test

Script
Var myArr = new Array ()

Function fillRnd (arrLen) {// enter random numbers ranging from 1 to 10 in arrLen to the array.
For (var I = 0, arr = []; I arr [I] = Math. ceil (Math. random () * 10)
}
Return arr
}

Function getMax (arr ){
Var arrLen = arr. length;
For (var I = 0, ret = arr [0]; I ret = Math. max (ret, arr [I]);
}
Return ret;
}

Function getMax2 (arr ){
Return Math. max. apply (null, arr)
}


MyArr = fillRnd (20*10000) // generate 0.2 million random numbers and fill them in the array

Var t1 = new Date ()
Var max1 = getMax (myArr)
Var t2 = new Date ()
Var max2 = getMax2 (myArr)
Var t3 = new Date ()

If (max1! = Max2) alert ("error ")
Alert ([t3-t2, t2-t1]) // on my machine 96,464. Different machines may have different results

Script


By comparing 0.2 million pieces of data, the getMax2 time is 96 ms while the getmax time is 464. The difference is 5 times


Another example is the array push method.
Var arr1 = [1, 3, 4];
Var arr2 = [3, 4, 5];
If we want to expand arr2, append it to arr1 one by one, and finally let arr1 = [1, 3, 4, 5]
Arr1.push (arr2) obviously does not work. In this case, [, 4, [, 5] is obtained.

We can only use one loop to push one by one (of course, we can also use arr1.concat (arr2), but the concat method does not change the arr1 itself)
Var arrLen = arr2.length
For (var I = 0; I arr1.push (arr2 [I])
}

With Apply, things have become so simple.

Array. prototype. push. apply (arr1, arr2)

Related Article

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.