Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Quick Sin Cos vs Math Sin Cos vs Fastest
(version: 0)
Comparing performance of:
Math vs Faster vs Fastest
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
let factorialize = function (num) { if (num === 0 || num === 1) return 1; for (let i = num - 1; i >= 1; i--) { num *= i; } return num; } let FACT_2 = 1 / factorialize(2); let FACT_3 = 1 / factorialize(3); let FACT_4 = 1 / factorialize(4); let FACT_5 = 1 / factorialize(5); let FACT_6 = 1 / factorialize(6); let FACT_7 = 1 / factorialize(7); const HALF_PI = Math.PI * 0.5; const DOUBLE_PI = Math.PI * 2; var mysin = function (angle) { let pow2 = angle * angle; let pow4 = pow2 * pow2; let pow6 = pow4 * pow2; return 1 - pow2 * FACT_2 + pow4 * FACT_4 - pow6 * FACT_6; } var mycos = function (angle) { return mysin(angle + HALF_PI); } var fastSin = function (x) { if (x < -Math.PI) x += DOUBLE_PI; else if (x > Math.PI) x -= DOUBLE_PI; if (x < 0) return 1.27323954 * x + 0.405284735 * x * x; else return 1.27323954 * x - 0.405284735 * x * x; } var fastCos = function (x) { return fastSin(x + HALF_PI); }
Tests:
Math
Math.cos(0.4292); Math.sin(0.2321);
Faster
mycos(0.4292); mysin(0.2321);
Fastest
fastCos(0.4292); fastSin(0.2321);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Math
Faster
Fastest
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
5 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/143.0.0.0 Safari/537.36
Browser/OS:
Chrome 143 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Math
154346448.0 Ops/sec
Faster
91880672.0 Ops/sec
Fastest
65371072.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Benchmark Overview** The provided benchmark measures the performance of three different implementations of sine and cosine functions in JavaScript: `Math.sin`, `mycos` (custom implementation), and `fastSin` (optimized implementation). The benchmark tests these functions on specific angles, resulting in a total of 6 test cases. **Options Compared** 1. **Math.sin**: The standard JavaScript function for calculating the sine of an angle. 2. **Mycos**: A custom implementation of the sine function, which uses a Taylor series expansion to calculate the value. 3. **FastSin**: An optimized implementation of the sine function, which uses a lookup table and some mathematical tricks to reduce the number of calculations required. **Pros and Cons** 1. **Math.sin**: * Easy to implement and understand. * Works well for most use cases, but can be slow for large angles or high-performance applications. 2. **Mycos**: * Provides a good balance between performance and simplicity. * The Taylor series expansion used in Mycos is a common technique for approximating trigonometric functions. * However, the implementation may not be as efficient as FastSin for very large angles or high-performance applications. 3. **FastSin**: * Optimized for high-performance applications, using a lookup table and mathematical tricks to reduce calculations. * May be more complex to implement and understand, especially for those without experience with optimization techniques. * Provides better performance than Mycos and Math.sin for large angles or high-performance applications. **Library: FastSin** The `fastSin` implementation uses a lookup table to store pre-calculated values of the sine function for common angles. This allows the function to return the result directly, without performing any calculations. The lookup table is implemented using a mathematical trick involving the angle and its negative counterpart. **Special JS Feature/Syntax: None** There are no special JavaScript features or syntax used in this benchmark. **Other Alternatives** 1. **Brent's Method**: Another optimization technique for calculating trigonometric functions, which uses a combination of Taylor series expansions and polynomial approximations to reduce calculations. 2. **FasterSin library**: A third-party implementation of the sine function optimized for high-performance applications, using techniques such as vectorization and SIMD instructions. **Benchmark Preparation Code** The benchmark preparation code defines several functions: 1. `factorialize`: Calculates the factorial of a given number using a simple loop. 2. `mycos` and `mysin`: Implementations of the sine and cosine functions using Taylor series expansions, respectively. 3. `fastSin` and `fastCos`: Optimized implementations of the sine and cosine functions using lookup tables and mathematical tricks. The benchmark preparation code also defines some constants, such as `HALF_PI` and `DOUBLE_PI`, which are used to calculate angles in radians.
Related benchmarks:
Quick Sin Cos vs Math Sin Cos
trig funcs vs trigids
fast sin cos vs math sin cos
Trig functions plusplus
Comments
Confirm delete:
Do you really want to delete benchmark?