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)
(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
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 (Windows NT 10.0; Win64; x64; rv:142.0) Gecko/20100101 Firefox/142.0
Browser/OS:
Firefox 142 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Math.atan2
1558175.8 Ops/sec
fast approx. atan2
1465684.6 Ops/sec
cached Math.atan2
1445157.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Benchmark Overview** The provided benchmark compares the performance of three different implementations of the `atan2` function in JavaScript: 1. **Native `Math.atan2`**: The built-in implementation of `atan2` in the JavaScript engine. 2. **Fast Approximation (`fastAtan2`)**: A custom implementation written by the test author, optimized for speed using a combination of mathematical approximations and variable handling. 3. **Cached `Math.atan2`**: An implementation that caches the results of previous calls to `Math.atan2`, potentially improving performance by avoiding redundant calculations. **Comparison Options** The benchmark compares these three options because they represent different approaches to implementing `atan2`: * **Native `Math.atan2`**: The built-in implementation is often a good starting point, but its performance might not be optimized for specific use cases or hardware. * **Fast Approximation (`fastAtan2`)**: This custom implementation uses mathematical approximations and variable handling to achieve better performance. However, it may require more maintenance and tuning compared to the native implementation. * **Cached `Math.atan2`**: Caching previous results can improve performance by reducing redundant calculations, but it may increase memory usage and lead to cache misses if not implemented carefully. **Pros and Cons** Here are some pros and cons of each approach: * **Native `Math.atan2`**: + Pros: Well-maintained, widely supported, and optimized for most use cases. + Cons: May have performance limitations due to the engine's internal optimizations or hardware dependencies. * **Fast Approximation (`fastAtan2`)**: + Pros: Can achieve better performance in specific use cases or on certain hardware platforms. + Cons: Requires more maintenance, tuning, and expertise to ensure accurate results and optimal performance. * **Cached `Math.atan2`**: + Pros: Can improve performance by reducing redundant calculations and avoiding cache misses. + Cons: May increase memory usage, lead to cache invalidations, or introduce additional complexity. **Library Usage** In this benchmark, the library used is the JavaScript engine's implementation of `Math.atan2`. There are no external libraries mentioned. **Special JS Features or Syntax** There are no special JavaScript features or syntax used in these implementations. They only utilize standard JavaScript functions and variables. **Alternatives** Some alternatives to consider when implementing `atan2` include: * Using a library like `mathjs` or `lodash.math` that provides optimized implementations of mathematical functions, including `atan2`. * Implementing a custom caching mechanism using JavaScript's built-in `Map` or `WeakMap` data structures. * Considering the use of hardware acceleration or SIMD instructions for performance-critical applications. Keep in mind that the choice of implementation depends on the specific requirements and constraints of your project.
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 + local scope)
Fast atan2 vs Math.atan2
Comments
Confirm delete:
Do you really want to delete benchmark?