var N = 1000000;
var x = new Float32Array(N);
var y = new Float32Array(N);
var theta = new Float32Array(N);
for (var i = 0; i < N; ++i) {
x[i] = 100* Math.random();
y[i] = 100* Math.random();
}
for (var i = 0; i < N; ++i) {
theta[i] = 2 * Math.PI * Math.random();
}
var fn = Math.sin;
for (var i = 0; i < N; ++i) { fn(theta[i]); }
var fn = Math.cos;
for (var i = 0; i < N; ++i) { fn(theta[i]); }
var fn = Math.tan;
for (var i = 0; i < N; ++i) { fn(theta[i]); }
var fn = Math.asin;
for (var i = 0; i < N; ++i) { fn(x[i]); }
var fn = Math.acos;
for (var i = 0; i < N; ++i) { fn(x[i]); }
var fn = Math.atan;
for (var i = 0; i < N; ++i) { fn(x[i]); }
var fn = Math.atan;
for (var i = 0; i < N; ++i) { fn(x[i], y[i]); }
for (var i = 0; i < N; ++i) { i/5 }
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
sin | |
cos | |
tan | |
asin | |
acos | |
atan | |
atan2 | |
division |
Test name | Executions per second |
---|---|
sin | 12.4 Ops/sec |
cos | 12.7 Ops/sec |
tan | 12.5 Ops/sec |
asin | 12.3 Ops/sec |
acos | 12.4 Ops/sec |
atan | 12.6 Ops/sec |
atan2 | 8.1 Ops/sec |
division | 24.6 Ops/sec |
Let's break down what's being tested in this benchmark.
Benchmark Overview The test is designed to measure the performance of various trigonometric functions (sin, cos, tan, asin, acos, atan, and atan2) on different inputs.
Script Preparation Code
The script preparation code generates three arrays of length N
containing random floating-point numbers. These arrays are used as input values for the trigonometric functions being tested.
Test Cases Each test case is defined by a "Benchmark Definition" string that specifies the trigonometric function to be tested. The tests are:
Trigonometric Functions
Math.sin()
: calculates the sine of a given angle in radians.Math.cos()
: calculates the cosine of a given angle in radians.Math.tan()
: calculates the tangent of a given angle in radians.Math.asin()
: calculates the arc sine (inverse sine) of a given value in radians. Note that this function returns an angle in the range [-π/2, π/2].Math.acos()
: calculates the arc cosine (inverse cosine) of a given value in radians. Note that this function returns an angle in the range [0, π].Math.atan()
: calculates the arctangent (inverse tangent) of a given value in radians.Math.atan2()
: calculates the arctangent of two values using the "divide by zero" trick.Performance Comparison The benchmark measures the performance of each trigonometric function across different browsers, operating systems, and devices. The results are displayed as executions per second (FPS), which indicates how many times a function is executed in one second.
Library Usage
None of the test cases use any external libraries besides the built-in Math
library.
Special JS Features or Syntax No special JavaScript features or syntax are being tested in this benchmark. The tests focus solely on the performance of the trigonometric functions themselves.
Performance Results The performance results show that, generally speaking, the more efficient function is atan2, while sin and cos have relatively similar performance across different browsers and operating systems. However, it's essential to note that these results might vary depending on the specific hardware, software, and usage patterns being tested.
In conclusion, this benchmark aims to measure the performance of various trigonometric functions in JavaScript, providing a comprehensive understanding of their efficiency across different platforms and scenarios.