Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Quick Sin Cos vs Math Sin Cos
(version: 0)
Comparing performance of:
Math vs Faster
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; 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); }
Tests:
Math
Math.cos(0.4292); Math.sin(0.2321);
Faster
mycos(0.4292); mysin(0.2321);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Math
Faster
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.0.1 Safari/605.1.15
Browser/OS:
Safari 18 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Math
10254429.0 Ops/sec
Faster
8538946.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll explain the benchmark and its components in detail. **Benchmark Overview** MeasureThat.net is a website where users can create and run JavaScript microbenchmarks to compare the performance of different implementations. The provided JSON represents two individual test cases: "Quick Sin Cos vs Math Sin Cos" and "Math". Each test case has a unique implementation, which we'll analyze below. **Test Case 1: Quick Sin Cos** This benchmark tests the performance of a custom implementation of `sin` and `cos` functions using a special technique called "factorialization." The factorialization method is used to compute trigonometric values like sin and cos for given angles. This approach involves computing intermediate results (e.g., `pow2`, `pow4`) and combining them to produce the final result. The custom implementation uses a recursive function `factorialize` to calculate factorials, which are then used in the trigonometric calculations. The `mysin` and `mycos` functions use this factorialization approach to compute sin and cos values. **Test Case 2: Math** This benchmark compares the performance of the built-in JavaScript `Math.cos` and `Math.sin` functions with their custom implementations (`mycos` and `mysin`, respectively). The built-in functions are expected to be optimized for performance. **Comparison Options** The two test cases compare different approaches: 1. **Quick Sin Cos vs Math Sin Cos**: This benchmark compares the custom implementation using factorialization (Test Case 1) with the built-in JavaScript trigonometric functions (`Math.cos` and `Math.sin`, Test Case 2). * Pros: Custom implementation can be optimized for specific use cases or hardware. * Cons: May require more memory allocation, and the factorialization method can be slower than using built-in functions for small input values. 2. **Math**: This benchmark compares the performance of the built-in JavaScript trigonometric functions (`Math.cos` and `Math.sin`) with their custom implementations (`mycos` and `mysin`, Test Case 1). **Library: Math.js (Not Used)** Note that there is a mention of a library called "Math.js" in the provided JSON, but it's not used in either test case. It seems like this was an abandoned part of the benchmark. **Special JS Feature:** The custom implementation `mysin` and `mycos` use a technique called "power reduction" to compute trigonometric values more efficiently. This method involves using the double-angle formulas for sine and cosine, which reduce the number of multiplications required to compute these values. Power reduction is a common optimization technique used in many mathematical libraries and implementations, as it can significantly improve performance by reducing the number of multiplications. **Alternatives** Other alternatives for implementing trigonometric functions include: 1. **Half-angle formulas**: These formulas can be used to compute sine and cosine values more efficiently than using double-angle formulas. 2. **Fast Fourier Transform (FFT)**: FFT can be used to efficiently compute trigonometric values, but it may require more complex code and is typically used for larger datasets. These alternatives may offer better performance or accuracy in certain cases, but they also come with their own trade-offs in terms of complexity, memory usage, and implementation challenges.
Related benchmarks:
pi 180
pi 180
Quick Sin Cos vs Math Sin Cos vs Fastest
fast sin cos vs math sin cos
Comments
Confirm delete:
Do you really want to delete benchmark?