Home > front end >  Js lists for loop performance [ultra-high scattered points]
Js lists for loop performance [ultra-high scattered points]

Time:09-27

Requirements:
Data from a 300000 a one-dimensional array of interval by 20 points, take 20 points of maximum and minimum values, the median, and according to the order of the original array to generate new three points (i.e., 20 points into three points); Due to take the value of 20, intermediate 2 value take 1 can, if the maximum value, minimum value, the value of the same, take a can, but to ensure that pick up the 3 values in 20 points in the original sequence is consistent; Finally to generate a new array of 45000 data:
Between the current code takes about 550 ms, hope to be able to optimize performance, reduce js execution time; Code the less time-consuming, I give the higher points, thank you for your help.

At 20 points, for example:
,67,34,2,56,123,66,77,88,8,22,187,1,99,88,77,34,2,1,99 [5] should get [66187, 1] or [56187, 1] can be

The current optimization code:
 
Const generateRandomData=https://bbs.csdn.net/topics/(total=4000, Max=200, min=10)=> {//random array with total length, every number in the array is between min and Max
Const arr=[]
For (the let I=0; i Arr=min + ~ ~ [I] (Math) random () * (Max - min + 1))
}
Return arr
}

Const getThreeValDataByCompleteData=https://bbs.csdn.net/topics/(20) data, step==> {//access to meet the demand points array
Let resArr=[]
For (the let I=0; i Let arr=data. Slice (I, I + step),
Arrlen=arr. Length,
SortArr=arr. Concat (). Sort ((a, b)=& gt; A - b),
Max=sortArr [] arrlen - 1,
Min=sortArr [0],
Center=sortArr [Math ceil (arrlen/2)]
Let num=3, n=0, threeValArr=Array (num)
Let maxExist=true, minExist=true, centerExist=true
For (let j=0; J & lt; Arrlen; J++) {
If (arr [j]===Max & amp; & MaxExist) {
MaxExist=false
ThreeValArr [n++]=arr [j]
}
Else if (arr [j]===min & amp; & MinExist) {
MinExist=false
ThreeValArr [n++]=arr [j]
}
Else if (arr [j]===center & amp; & CenterExist) {
CenterExist=false
ThreeValArr [n++]=arr [j]
}
If (n===num) break;
}
ResArr=resArr. Concat (threeValArr)
}
Return resArr
}

Let the interval=20//how many points each generation interval array
Let data=https://bbs.csdn.net/topics/generateRandomData (300000, interval)//random length of 300000 array, each number in the array for between 10 and 200
The console. Time ()
Let res=getThreeValDataByCompleteData (data)//through function for 45000 to meet the requirements of point
The console. TimeEnd ()
The console. The log (` randomly generated ${data. Length}, ${interval} points as interval array, each interval array in order to get the minimum, maximum, the average of the three points (order according to the order of the three values in the array actually), the last generated ${res. Length}, which took the millisecond value, `)


CodePudding user response:

A line parameters interval and wrong, the following code
 
Let the interval=20//how many points each generation interval array
Let data=https://bbs.csdn.net/topics/generateRandomData (300000)//random length of 300000 array, each number in the array for between 10 and 200
The console. Time ()
Let res=getThreeValDataByCompleteData (data, the interval)//through function for 45000 to meet the requirements of point
The console. TimeEnd ()
The console. The log (` randomly generated ${data. Length}, ${interval} points as interval array, each interval array in order to get the minimum, maximum, the average of the three points (order according to the order of the three values in the array actually), the last generated ${res. Length}, which took the millisecond value, `)

  • Related