Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
math.sin vs fast sin vs cached math.sin
(version: 0)
Comparing performance of:
Math.sin vs fast sin vs cachedSin
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
const PI = Math.PI; const TAU = PI * 2; const B = 4 / PI; const C = -4 / TAU; var cachedSin = Math.sin; var sin = x => { return (B * x + C * x * ((x < 0) ? -x : x)); };
Tests:
Math.sin
let k = []; for (let i = 0; i < 100; ++i) k.push(Math.sin(i));
fast sin
let k = []; for (let i = 0; i < 100; ++i) k.push(sin(i));
cachedSin
let k = []; for (let i = 0; i < 100; ++i) k.push(cachedSin(i));
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Math.sin
fast sin
cachedSin
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
3 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:146.0) Gecko/20100101 Firefox/146.0
Browser/OS:
Firefox 146 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Math.sin
555546.1 Ops/sec
fast sin
2393000.8 Ops/sec
cachedSin
587040.2 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
The provided benchmark compares the performance of three different approaches to calculate the sine function: 1. **Native `Math.sin()`**: This is the built-in JavaScript function for calculating the sine of an angle, implemented in C++ by Mozilla. 2. **Custom "fast sin" function**: This is a custom implementation of the sine function, which uses a clever mathematical trick to reduce the number of multiplications required. The formula used is `B * x + C * x * ((x < 0) ? -x : x)`, where `B` and `C` are constants defined in the script preparation code. 3. **Cached `Math.sin()`**: This approach caches the result of a previous call to `Math.sin()`. In this benchmark, the variable `cachedSin` is assigned the value of `Math.sin()`, but with the actual calculation done only once during script preparation. **Options Compared:** * Native `Math.sin()` vs Custom "fast sin" function * Native `Math.sin()` vs Cached `Math.sin()` **Pros and Cons:** 1. **Native `Math.sin()`**: * Pros: + Widely supported by most browsers. + Optimized for performance by Mozilla. * Cons: + May not be as efficient due to overhead of browser implementation. 2. **Custom "fast sin" function**: * Pros: + Potentially faster than native `Math.sin()` due to reduced number of multiplications. * Cons: + Requires custom implementation, which may add complexity. + May not be supported by all browsers or JavaScript engines. 3. **Cached `Math.sin()`**: * Pros: + Can potentially be faster than native `Math.sin()` due to caching effect. * Cons: + Requires an extra variable to store the cached result, which can lead to memory usage. **Other Considerations:** * The custom "fast sin" function uses a mathematical trick that is specific to floating-point arithmetic. This trick may not work correctly for integer arithmetic or other data types. * The benchmark does not consider other factors like input range, precision, or rounding mode when comparing the performance of these approaches. **Libraries and Special JS Features:** * The custom "fast sin" function uses a library-like approach to implement its formula. However, it is not a traditional library, as it is embedded directly in the script. * There are no special JavaScript features used beyond the `let`, `const`, and arrow functions syntax. **Alternatives:** * Other optimization techniques for calculating sine, such as using polynomial approximations or specialized libraries like FFTW. * Using SIMD instructions to perform calculations on multiple values simultaneously, if supported by the browser or JavaScript engine. * Compiling custom JavaScript code to machine code using tools like WebAssembly, which can potentially lead to significant performance improvements.
Related benchmarks:
pi 180
pi 180
sin vs fast sin
fast sin cos vs math sin cos
Comments
Confirm delete:
Do you really want to delete benchmark?