The inner loop performs the same iteration as the outer loop, iterating all the points except the one where i === j. But you can exploit the symmetry between pairs of interactions: if you compute a force on i from j, then j will feel an equal but opposite force. So, the inner loop could actually be something like: for (j = 0; j < i; ...). Maybe it would even be faster to iterate the loop backwards from i for better memory locality. And then when you add in newVelos[i] make sure you subtract from newVelos[j]
https://github.com/pablo-mayrgundter/celestiary/blob/9eccfa56dace1835651ee510f705ce5f91b04ecd/js/Galaxy.js#L83-L86
The inner loop performs the same iteration as the outer loop, iterating all the points except the one where
i === j
. But you can exploit the symmetry between pairs of interactions: if you compute a force oni
fromj
, thenj
will feel an equal but opposite force. So, the inner loop could actually be something like:for (j = 0; j < i; ...)
. Maybe it would even be faster to iterate the loop backwards fromi
for better memory locality. And then when you add innewVelos[i]
make sure you subtract fromnewVelos[j]