var i;
var r;
var v = 2**16;
function test1() {
for(i=0; i < 1000; ++i) {
r = Math.sqrt(v);
}
}
function test2() {
for(i=0; i < 1000; ++i) {
r = Q_rsqrt(v) * v;
}
}
//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;
}
test1();
test2();
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Math.sqrt | |
Quakes fast inverse sqrt. |
Test name | Executions per second |
---|---|
Math.sqrt | 476831.6 Ops/sec |
Quakes fast inverse sqrt. | 1170.0 Ops/sec |
I'll break down the provided benchmark definition and test cases to explain what's being tested, compared, and their pros and cons.
Benchmark Definition
The benchmark is testing two JavaScript functions: test1()
and test2()
. The latter uses the Quakes fast inverse square root function, which is a custom implementation of the fast inverse square root algorithm. This algorithm is an optimization for calculating the square root in floating-point arithmetic, particularly useful for high-performance applications.
Functionality of test1()
test1()
calculates the square root of a large number (v
) using the built-in Math.sqrt()
function 1000 times. The purpose of this test is to evaluate how fast the Math.sqrt()
function executes.
Functionality of test2()
test2()
also calculates the square root of v
, but it uses the Quakes fast inverse square root function, which is a custom implementation that attempts to mimic the original fast inverse square root algorithm. This test aims to compare the performance of this custom implementation with the built-in Math.sqrt()
function.
Comparison
The benchmark compares the execution time of two approaches:
test1()
) measures the execution time of using the built-in Math.sqrt()
function.test2()
) measures the execution time of using the custom Quakes fast inverse square root function.Pros and Cons
Pros:
Cons:
Library: FastInverseSqrt
The FastInverseSqrt
library is a custom implementation of the fast inverse square root algorithm. It's likely that this library is designed for high-performance applications and provides an optimized solution for calculating square roots in floating-point arithmetic.
Other alternatives
In general, when it comes to calculating square roots in JavaScript:
However, it's essential to note that using custom implementations can add complexity and potential errors, so they should be used judiciously.
Special JS feature or syntax
This benchmark does not use any special JavaScript features or syntax beyond what's required for the implementation of the Quakes fast inverse square root function.