Through code profiling, I have found the Math.sqrt function specifically to be a major bottleneck in a large doubly-nested loop that runs every timestep in my program. Is there any way to improve its performance? Should I inline some kind of iterative calculation, or lookup table-based calculation?
Any help would be greatly appreciated!
I cannot replace it with a squares calculation instead, since it is not a comparison.
EDIT: The relevant portion of the code looks roughly as follows
var width = 2000;
var height = 2000;
function update() {
for (var j = 0; j < height; ++j) {
for (var i = 0; i < width; ++i) {
array[i][j] = Math.sqrt(/* some expression involving i and j */);
}
}
}
var fps = 60;
setInterval(update, 1000 / fps);