Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Fast approx. atan2 vs Math.atan2 vs cached atan2
(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; const FAT2 = { ax: 0.1, ay: 0.1 }; //variables for gc (v8 optimizations); let a = 0.1, s = 0.1, r = 0.1; var fastAtan2 = (y, x) => { FAT2.ax = abs(x); FAT2.ay = abs(y); a = min(FAT2.ax, FAT2.ay) / max(x, y); s = a * a; r = ((-0.0464964749 * s + 0.15931422) * s - 0.327622764) * s * a + a; if (FAT2.ay > FAT2.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
var k = [1, .23, 0.12, 929, 8172, 9.2, 21.2].map(e => Math.atan2(e, .5));
fast approx. atan2
var k = [1, .23, 0.12, 929, 8172, 9.2, 21.2].map(e => fastAtan2(e, .5));
cached Math.atan2
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:
one year ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64; rv:133.0) Gecko/20100101 Firefox/133.0
Browser/OS:
Firefox 133 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Math.atan2
5875750.5 Ops/sec
fast approx. atan2
7164206.0 Ops/sec
cached Math.atan2
5210515.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark definitions and compare the options. **Benchmark Definitions:** The test cases measure three different implementations of the `atan2` function: 1. **Original Math.atan2**: This is the standard implementation provided by JavaScript, which uses a more complex algorithm to calculate the inverse tangent of two arguments. 2. **Fast Approximation (fastAtan2)**: This implementation is a simplified approximation of the original `atan2` function, optimized for performance. It uses some V8-specific optimizations and caching techniques to reduce the number of calculations required. 3. **Cached Math.atan2**: This implementation is similar to the original `atan2` function but has been modified to cache its results for repeated calls with the same arguments. **Options Compared:** The three implementations differ in their approach: * Original `Math.atan2`: uses a complex algorithm, which provides high accuracy but lower performance. * Fast Approximation (`fastAtan2`): uses simplifications and optimizations to reduce performance overhead, but may sacrifice some accuracy. * Cached `Math.atan2`: caches results for repeated calls with the same arguments, reducing the number of calculations required. **Pros and Cons:** * **Original Math.atan2**: Pros: + High accuracy + Well-tested and maintained implementation + Suitable for most use cases where performance is not critical. * Cons: + Lower performance due to complex algorithm + May be slower than optimized implementations * **Fast Approximation (fastAtan2)**: Pros: + Improved performance due to simplifications and optimizations + Still provides good accuracy for most use cases * Cons: + Simplifications may lead to reduced accuracy in certain edge cases + Not suitable for all scenarios where high accuracy is required. * **Cached Math.atan2**: Pros: + Reduces performance overhead by caching results + Suitable for scenarios where repeated calls with the same arguments are common. * Cons: + May require additional memory allocation to store cached results + Cache may become outdated if implementation changes **Library and Syntax:** The test case uses a custom `fastAtan2` function that is defined in the "Script Preparation Code" section of the benchmark definition. This function is similar to the original `Math.atan2` function but with some optimizations applied. **Special JS Feature or Syntax:** There are no special JavaScript features or syntax used in this benchmark. The implementations focus on optimizing the performance and accuracy of the `atan2` function. **Other Alternatives:** If you need to measure other mathematical functions, consider using: * **BenchMarking frameworks**: like Benchmark.js or Microbenchmark * **JavaScript engines**: like V8 (used by Chrome) or SpiderMonkey (used by Firefox) * **Math libraries**: like Mathjs or NumJS These alternatives can provide more comprehensive benchmarking and insights into the performance of different JavaScript implementations.
Related benchmarks:
Fast approx. atan2 vs Math.atan2
Fast approx. atan2 vs Math.atan2 vs cached atan2 (v8 optimization buster)
Fast approx. atan2 vs Math.atan2 vs cached atan2 (v8 optimization buster + local scope)
Fast atan2 vs Math.atan2
Comments
Confirm delete:
Do you really want to delete benchmark?