So scrap Math.pow and just use conventional A * A, and when you say precalc radian to degree should that be put into a var PI = and then just call it when needed?
was considering just radians throughout as I think the servo control board can input radians.
@hotNipi Yeah its probably not needed to be fair the only reason I was going to covert it was to reduce the number length, but if it's going to have a negative effect on processing time them maybe leave it, but at least I know how to do it now.
Some other tips you may find useful
You can reduce variable creation. (f.e GR in inverse kinematics seems to not used in other calculations so put it straight into outgoing payload )
You can store and read multiple variables in context in one go
https://nodered.org/docs/user-guide/writing-functions#getset-multiple-values
Also there is https://flows.nodered.org/node/node-red-contrib-unsafe-function. Claims to be fast and furious. Don't know details but may do the the thing for you. Worth a try.
@hotNipi just tried the unsafe function and that reduced the CPU below 10% so shaved off a massive amount, along with a raspberry pi 4 that should decrease more.
You probably should also avoid repeated calls to expensive math functions. For example, in the X-axis rotation matrix,
var cosx = Math.cos(Xrot);
var sinx = Math.sin(Xrot);
var Y1 = l1y*cosx-l1z*sinx;
var Z1 = l1z*cosx+l1y*sinx;
could be up to twice as fast as what you are doing now. It can be tempting (and more readable) to just code up the textbook equations, but if performance is an issue, think like a computer not a human.