我正試圖完成lulz的CodeWars挑戰,但我似乎無法獲得足夠的性能以通過提交。該解決方案是正確的,並通過了所有測試,但它未能通過性能測試,取> 120000毫秒如何優化/調試nonperformant javascript代碼
我的2個問題FOR YOU
人應該如何調試性能問題?我不知道如何開始調試性能和識別非代碼代碼,或者如何故意優化代碼
這個特定的代碼有什麼問題?有沒有一種模式,我正在執行不正確?是否有一些代碼發生了太多次?
詳細要求可以在這裏找到: https://www.codewars.com/kata/integers-recreation-one ```
//generate an array of range, containing every number M to N
//map1: for each number find all divisors
//map2: for each array of divisors, format answer for tests
//filter out undefined results
let listSquared = (m, n) => range(m,n)
.map(nextNumberInRange => findDivisors(nextNumberInRange))
.map(arrayOfDivisors => formatAnswer(
arrayOfDivisors[arrayOfDivisors.length - 1],
squareAndSumAll(arrayOfDivisors)))
.filter(x => x !== undefined)
//if the square root of y (the sum of squared divisors) is WHOLE, return [x,y]
let formatAnswer = (x,y) => Math.sqrt(y) % 1 === 0 ? [x,y] : undefined
//find all divisors of any integer
let findDivisors = (x) => range(1,x).filter(y => x%y === 0 || y===x)
//generate an array containing values from start to end.
//e.g. 100-500, 351-293487 etc.
let range = (start,end) => [...Array((end-start)+1)].map((x,i)=> start+i)
let squareAndSumAll = (x) => x.map(square).reduce(add)
let add = (x,y) => x + y
let square = (x) => x * x
詳細要求可以在這裏找到: https://www.codewars.com/kata/integers-recreation-one
你應該能夠代碼摑到窗口在他們的網站上,並重新創建通過輸入和失敗的計時器測試。