My script is running on a page that runs other scripts which I do not control.
I am running a calculation that takes approximately 20ms to run. (I process a certain list when I init the script and I measure the time per item, and so I can aim to process a list of X elements so that it takes 20ms, on whichever device I am running).
My aim is to impact performance and interactiveness as little as possible, therefore I do not want my calculation to run in the event-loop if it will make it run for longer than 50ms (long task).
I do not know the other calculations that are going to run in this event loop, so I don't know whether I run my 20ms calculation when 40ms were already used in the event loop, or on an event-loop that took only 10ms and has no more tasks, so my calculation will be okay to run in it.
Is there a way for me to make sure my 20ms calculation does not prolong the event-loop over 50ms before I run it?
Notes
- Can't use Web Workers (my calculation traverses the DOM tree)
- setTimeout(0,..) is not an option cause when the callback is called I still do not know whether I have enough time on the event loop to run my calculation.
- my calculation just gathers data about the current state of the DOM. It doesn't update it - so I don't need the render state to be updated.
- I don't expect to have 0 impact. I am doing a heavy calculation so there's a price to pay. I just want to find the best method to minimize the impact on: TBT (Total Blocking Time) in the main thread.