nowadays i am optimizing some js code.
there is a function named appendXYZ,and it is invoked in a loop with other functions.
it looks like as the following:
function OuterFunc (){
for(...){// about 150 times
...
appendXYZ();
//other dependent functions
...
}
}
and now i am pretty sure that appendXYZ cause high cpu usage – it can reach 50%,
but if i remove this function,cpu usage is only 1%.
when the cpu usage is 50%,the browser is nearly frozen and the page is lack of responsiveness.
what is more ,the OuterFunc execute every 20 seconds and appendXYZ is from a third party script code and i cant modify it.
so how to optimize this code?
now i am trying to use setTimeout but i dont know whether it works.
I don’t know what that function does, but you could try making its invocation asynchronous.
It may or may not work, and it will still require the same amount of CPU, but it should at least free up the browser a bit.
Again this may break the function. Can’t tell without seeing more code.
If you’re passing arguments, then you’d need something like: