Skip to content

Instantly share code, notes, and snippets.

@starfys
Last active January 11, 2024 22:04
Show Gist options
  • Star 15 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save starfys/aaaee80838d0e013c27d to your computer and use it in GitHub Desktop.
Save starfys/aaaee80838d0e013c27d to your computer and use it in GitHub Desktop.
Fast inverse square root in Javascript
//Based on the fast inverse square root function
// https://en.wikipedia.org/wiki/Fast_inverse_square_root
// Some original comments preserved for humor value
// Designed to try to mimic the original as closely as possible
function Q_rsqrt(number)
{
var i;
var x2, y;
const threehalfs = 1.5;
x2 = number * 0.5;
y = number;
//evil floating bit level hacking
var buf = new ArrayBuffer(4);
(new Float32Array(buf))[0] = number;
i = (new Uint32Array(buf))[0];
i = (0x5f3759df - (i >> 1)); //What the fuck?
(new Uint32Array(buf))[0] = i;
y = (new Float32Array(buf))[0];
y = y * ( threehalfs - ( x2 * y * y ) ); // 1st iteration
// y = y * ( threehalfs - ( x2 * y * y ) ); // 2nd iteration, this can be removed
return y;
}
@geocar
Copy link

geocar commented Feb 7, 2016

If you don't call new Float32Array and new Uint32Array so much, it's about twice the speed of 1/Math.sqrt(x):

  • isqrt: 1.237ms

  • Q_rsqrt: 11.280ms

  • q2: 0.647ms

    var buf = new ArrayBuffer(4),
        f32=new Float32Array(buf),
        u32=new Uint32Array(buf);
    function q2(x) {
      var x2 = 0.5 * (f32[0] = x);
      u32[0] = (0x5f3759df - (u32[0] >> 1));
      var y = f32[0];
      y  = y * ( 1.5 - ( x2 * y * y ) );   // 1st iteration
      return y;
    }
    

@Olian04
Copy link

Olian04 commented Oct 3, 2017

@geocar Q_rsqrt is now allot slower than the default implementation (at-least in chrome).
https://repl.it/MA07/8

@ingvardm
Copy link

ingvardm commented Aug 28, 2021

const buf = new ArrayBuffer(4)
const f32 = new Float32Array(buf)
const u32 = new Uint32Array(buf)

function fisr(x) {
	const x2 = 0.5 * (f32[0] = x)
	u32[0] = (0x5f3759df - (u32[0] >> 1))
	let y = f32[0]
	y = y * (1.5 - (x2 * y * y))
	return y
}

const itterations = 999999999

function benchFisr(){
	for(let i = 0; i < itterations; i++){
		(fisr(i + 1))
	}
}

function benchSqrt() {
	for (let i = 0; i < itterations; i++) {
		(1/Math.sqrt(i + 1))
	}
}

function benchmark(){
	let startTS = Date.now()
	benchFisr()
	console.log('fisr: ',Date.now() - startTS, 'ms')
	
	startTS = Date.now()
	benchSqrt()
	console.log('1/sqrt: ',Date.now() - startTS, 'ms')
}

benchmark()

nodejs 14:

fisr:  982 ms
1/sqrt:  496 ms

chrome 92

fisr:  1573 ms
1/sqrt:  747 ms

am i missing something, or that the sqrt implementation on x86 is just that much better?
(hint: it is)

@AhmedRahmi
Copy link

no your using to many iterations

@ingvardm
Copy link

no your using to many iterations

Please, take a minute to try to understand the code before you post your reply, thank you.

@valen214
Copy link

moving computation impl to native is better most of the time, that's why they introduced WASM.

@emre-aki
Copy link

@ingvardm Here's my take on it, fisr seems to become faster in higher orders of magnitude of iterations.

const buffer = new ArrayBuffer(4);
const ui32 = new Uint32Array(buffer);
const f32 = new Float32Array(buffer);

function Q_rsqrt (number)
{
    f32[0] = number;
    ui32[0] = 0x5F3759DF - (ui32[0] >> 1);
    const x = f32[0];
    return x * (1.5 - 0.5 * x * x * number);
}

let qTotal = 0, sTotal = 0;
const iters = 999999999;

for (let i = 0; i < iters; ++i)
{
    const s = Date.now();
    Q_rsqrt(i + 1);
    qTotal += Date.now() - s; 
}

for (let i = 0; i < iters; ++i)
{
    const s = Date.now();
    (1 / Math.sqrt(i + 1));
    sTotal += Date.now() - s; 
}

console.log("Q_rsqrt:", qTotal);       // Q_rsqrt: 34027
console.log("1 / Math.sqrt:", sTotal); // 1 / Math.sqrt: 34372

@Anticubex
Copy link

So, at least in terms of javascript, this seems to be a problem of theoretical vs practical time complexity. Q_rsqrt is probably faster per iteration, but the additional overhead of the buffers and such causes native implementations to be faster for most use cases. But if you do happen to be running a billion calls to inverse square root, you probably shouldn't be using naked javascript. As valen214 said, WASM is probably a better investment if you're chasing performance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment