Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Fast approx. atan2 vs Math.atan2 vs cached atan2 (v8 optimization buster + local scope)
(version: 0)
Comparing performance of:
Math.atan2 vs fast approx. atan2 vs cached Math.atan2
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
const abs = Math.abs, min = Math.min, max = Math.max, PI = Math.PI, PI2 = PI / 2; var fastAtan2 = (y, x) => { let ax = abs(x); let ay = abs(y); let a = min(ax, ay) / max(x, y); let s = a * a; let r = ((-0.0464964749 * s + 0.15931422) * s - 0.327622764) * s * a + a; if (ay > ax) r = PI2 - r; if (x < 0) r = PI - r; if (y < 0) r = -r; return r; }; //this is the fastest i can get it rn var atan2 = Math.atan2;
Tests:
Math.atan2
eval(''); var k = [1, .23, 0.12, 929, 8172, 9.2, 21.2].map(e => Math.atan2(e, .5));
fast approx. atan2
eval(''); var k = [1, .23, 0.12, 929, 8172, 9.2, 21.2].map(e => fastAtan2(e, .5));
cached Math.atan2
eval(''); var k = [1, .23, 0.12, 929, 8172, 9.2, 21.2].map(e => atan2(e, .5));
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Math.atan2
fast approx. atan2
cached Math.atan2
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
7 months ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64; rv:143.0) Gecko/20100101 Firefox/143.0
Browser/OS:
Firefox 143 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Math.atan2
2813293.8 Ops/sec
fast approx. atan2
3422825.2 Ops/sec
cached Math.atan2
2823459.8 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript benchmarks. **Benchmark Overview** The provided benchmark measures the performance of three different implementations of the `atan2` function: 1. `fast approx. atan2`: A custom implementation that uses an optimized formula to calculate `atan2`. 2. `cached Math.atan2`: The built-in `Math.atan2` function with caching enabled. 3. `Math.atan2`: The standard built-in `Math.atan2` function without any optimizations or caching. **Options Compared** The benchmark compares the performance of these three implementations: * **Custom optimization**: `fast approx. atan2` uses an optimized formula to calculate `atan2`. This implementation is likely to be faster than the standard `Math.atan2` function. * **Caching**: `cached Math.atan2` enables caching, which can improve performance by storing the results of previous calculations and reusing them for subsequent calls with the same arguments. However, this approach also introduces additional overhead due to cache management. * **Standard implementation**: The standard `Math.atan2` function without any optimizations or caching. **Pros and Cons** Here's a brief overview of the pros and cons of each approach: * **Custom optimization (fast approx. atan2)**: * Pros: Optimized formula can lead to significant performance improvements. * Cons: The implementation may not be as straightforward or maintainable as other approaches. * **Caching (cached Math.atan2)**: * Pros: Can provide good performance benefits, especially for frequent calls with the same arguments. * Cons: Introduces additional overhead due to cache management and may not perform well for infrequent calls. * **Standard implementation (Math.atan2)**: * Pros: Widely supported, well-maintained, and easy to understand. * Cons: May have slower performance compared to optimized implementations. **Library Usage** None of the benchmarked functions use any external libraries. However, `fast approx. atan2` is a custom implementation that uses some mathematical tricks to optimize the calculation. **Special JS Feature or Syntax** The benchmark does not use any special JavaScript features or syntax. The code is relatively straightforward and uses standard JavaScript constructs. **Alternatives** Some alternative approaches for implementing `atan2` could include: * Using a library like [Fast Atan2](https://github.com/greggman/fast-atan2), which provides an optimized implementation of the function. * Implementing `atan2` using a more advanced mathematical approach, such as [Taylor series expansion](https://en.wikipedia.org/wiki/Taylor_series). * Using JIT compilers or other optimization techniques to improve performance.
Related benchmarks:
Fast approx. atan2 vs Math.atan2
Fast approx. atan2 vs Math.atan2 vs cached atan2
Fast approx. atan2 vs Math.atan2 vs cached atan2 (v8 optimization buster)
Fast atan2 vs Math.atan2
Comments
Confirm delete:
Do you really want to delete benchmark?