JavaScript Performance Math.sqrt

Using code profiling, I found that the Math.sqrt function is the main bottleneck in a large binary-nested loop that runs every timestep in my program. Is there any way to improve its performance? Should I embed some kind of iterative calculation or calculate a table based on a table?

Any help would be greatly appreciated!

Instead, I cannot replace it with quadratic calculation, since this is not a comparison.

EDIT: The corresponding piece of code looks something like this

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);
+4
source share
1

, ~ 2.

:

x j, 3-3, 0, :

  • 0 * 1 1 * 0
  • 0 * 2 2 * 0
  • 1 * 2 2 * 1

0x0 1x0 2x0

0x1 1x1 2x1

0x2 1x2 2x2

0

All Articles