Relative distance from -180 to +180 (a and b can be millions of degrees apart, i.e. have winding):
(b - a) % 360
Absolute distance from 0 to +180 (a and b can be millions of degrees apart, i.e. have winding):
Math.abs((b - a) % 360)
Don't overcomplicate what doesn't need to be complicated.