When I’m running a computation heavy simulation in Chrome, it runs blazingly fast when the animation loop is ticking. However, sometimes, I need to calculate stuff outside of the animation loop, and regardless of whether I do this in a web worker or in the main thread with setInterval or requestAnimation frame, for that matter, these calculations don’t run anywhere near as fast as they would have if I ran them in the animation loop: it appears Chrome is throttling the speed at which these kinds of computations can be run, so they take ages to finish. I understand Chrome does this for good reasons, and I could circumvent the problem by running my calculations in the animation loop, but that just seems like an anti pattern of the third degree. Is there a way I can stop Chrome from throttling my heavy calculations? Or should I just do them server side?
requestAnimationFrame is the way to go then, why would you want to do this with any another approach?
It’s to do with how I manage state; I’d have to clone a bunch of data, introduce a couple of conditions and so on in the animation loop, which I’m reluctant to do. I’d rather just separate these calculations from it. Naturally, if there’s no way around it, I’ll just have to take my medicine, I guess, and do it, Not particularly keen on paying for having a node api up and running to generate this data, to be frank.
So, I solved my problem, and the solution was to simply ask myself what exactly I needed, and then asking myself how I could get that with the minimum amount of effort. The problem I was trying to solve was how to get a future snapshot of an n-body system where one mass dominates the rest, like our solar system where the Sun is 1000 heavier than the heaviest planet, Jupiter. Stupid as I were, it didn’t occur to me that on short time scales, perturbations are not an important factor in the trajectories of solar system bodies, so to get the future position of Jupiter, I would integrate the equations of motion for 11 solar system bodies, when I actually just needed 2 (!) - the Sun and Jupiter.
There are a few cases where perturbations matter, even in the short run, and for those cases I’ve added the ability to add perturbers, but usually we’re not talking about more than 1 body, so the gains in speed are still massive.
You’re all smart cookies, so I don’t think this applies to most of you, but if you, like me, have a tendency to get ahead of yourself instead of carefully analysing the problem, the latter will usually give you what you want