Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Math.min vs if - getting min value from array
(version: 0)
Comparing performance of:
if vs Math.min vs Cached min
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
arr = Array.from({ length: 10000 }, () => Math.random())
Tests:
if
let v = 2; for (const x of arr) { if (x < v) v = x; } return v;
Math.min
let v = 2; for (const x of arr) { v = Math.min(v, x); } return v;
Cached min
const min = Math.min; let v = 2; for (const x of arr) { v = min(v, x); } return v;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
if
Math.min
Cached min
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
8 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 YaBrowser/25.8.0.0 Safari/537.36
Browser/OS:
Yandex Browser 25 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
if
58882.6 Ops/sec
Math.min
36868.3 Ops/sec
Cached min
62366.7 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down what's being tested in this benchmark. **Benchmark Definition** The test is comparing three approaches to find the minimum value in an array: 1. **If statement**: Using an if statement to iteratively update the minimum value. 2. **Math.min function**: Using the built-in Math.min function to find the minimum value. 3. **Cached min**: Creating a variable `min` and assigning it the Math.min function, then using this cached function to find the minimum value. **Options Compared** The three options are compared in terms of execution speed. **Pros and Cons** 1. **If statement**: * Pros: Easy to understand and implement, doesn't require a separate variable. * Cons: Can be slow due to the iterative update process. 2. **Math.min function**: * Pros: Fast and efficient, as it's implemented in native code. * Cons: Requires importing the Math module or using the global `Math` object. 3. **Cached min**: * Pros: Faster than the if statement approach, as it reuses the same function call. * Cons: Requires creating a separate variable to cache the function. **Library** The test doesn't explicitly use any external libraries, but it does rely on the built-in Math module in JavaScript. The Math.min function is implemented in native code, which makes it fast and efficient. **Special JS Features or Syntax** There are no special JavaScript features or syntax used in this benchmark that would require explanation. **Other Alternatives** If you were to optimize the if statement approach further, you could consider using a more efficient algorithm like Boyer-Moore's algorithm for finding minimum values. However, this would add complexity and might not be necessary unless performance is critical. The `Math.min` function can't be optimized further without changing its implementation in native code. The `Cached min` approach remains as the best alternative to the if statement approach in terms of execution speed. Overall, this benchmark provides a simple yet informative comparison between three common approaches to finding minimum values in JavaScript arrays.
Related benchmarks:
arr.sort() vs. Math.min()
Math.min vs Array.sort[0]
Get max from an array of numbers (Math.max vs. iteration)
Math.min vs reduce
Array.sort vs Math.min 1
Comments
Confirm delete:
Do you really want to delete benchmark?