Home > Web Front-end > JS Tutorial > A brief discussion on Function.apply() Part 2------Using Apply's parameter array to improve JavaScript program performance_javascript skills

A brief discussion on Function.apply() Part 2------Using Apply's parameter array to improve JavaScript program performance_javascript skills

WBOY
Release: 2016-05-16 19:23:37
Original
1015 people have browsed it

Let’s talk about the skills of Function.apply() in improving program performance.

Let’s start with the Math.max() function. Math.max can be followed by any number of parameters, and finally returns the maximum value among all parameters.

For example
alert(Math.max(5,8)) //8
alert(Math.max(5,7,9,3,1,6)) //9

But in many cases, we need to find the largest element in an array.
var arr=[5,7,9,1]
alert(Math.max(arr)) // This is not possible. Be sure to write like this

function getMax(arr){
var arrLen=arr.length;
for(var i=0,ret=arr[0];i ret =Math.max(ret,arr[i]);                                                                                                                                                                                                                                                    . If you use apply, look at the code:
function getMax2(arr){
return Math.max.apply(null, arr)
}
The two pieces of code achieve the same purpose, but getMax2 Elegant, efficient and much simpler.



Look at the performance test:
getMax performance test


<script></font>var myArr=new Array()</p> <p> <font face="Verdana"><br>function fillRnd(arrLen){ //Fill arrLen random numbers from 1 to 10 into the array</font> for(var i=0,arr=[];i<arrLen;i ){</P> arr[i]=Math.ceil(Math.random()*10)<P> }<FONT face=Verdana> return arr<BR>}<BR><BR> <BR><BR>function getMax(arr){</FONT> var arrLen=arr.length;</P> for(var i=0,ret=arr[0];i<arrLen;i ){<P> ret =Math.max(ret,arr[i]);                                                                   <FONT face=Verdana><BR>function getMax2(arr){<BR> return Math.max.apply(null,arr)<BR>}<BR><BR><BR> </FONT></P>myArr=fillRnd(20*10000) //Generate 200,000 random numbers to fill in the array<P> <FONT face=Verdana>var t1=new Date()<BR>var max1=getMax(myArr)<BR>var t2=new Date()</FONT>var max2=getMax2(myArr)</P>var t3=new Date() <FONT face=Verdana> <P>if (max1!==max2) alert("error")<BR>alert([t3-t2,t2-t1]) //96,464 on my machine. Different machines may have different results</P> <P></script>


By comparing 200,000 data, the getMax2 time is 96ms and the getmax time is 464. The difference between the two is 5 times

Another example is the push method of an array.
var arr1=[1,3,4];

var arr2=[3,4,5];

If we want to expand arr2, then append to arr1 one by one, and finally let arr1 =[1,3,4,3,4,5]

arr1.push(arr2) Obviously it doesn’t work. Because doing this will get [1,3,4, [3,4,5] ]


We can only use a loop to push one by one (of course you can also use arr1.concat(arr2) but the concat method does not change arr1 itself)

var arrLen=arr2.length

for(var i =0;i arr1.push(arr2[i])
}

Since we have Apply , things have become so simple

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



source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template