Toggle navigation
MeasureThat
.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Math max/min vs Math.abs and %
(version: 0)
Comparing performance of:
Max min vs abs and %
Created:
6 years ago
by:
Guest
Go to the latest result
Script Preparation code:
this.number = Math.random() * 1000;
Tests:
Max min
return Math.max(250, Math.min(750, this.number));
abs and %
return Math.abs(this.number % 750)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Max min
abs and %
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
11 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:142.0) Gecko/20100101 Firefox/142.0
Browser/OS:
Firefox 142 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
abs and %
1161.5M/s
Max min
54.3M/s
View exact numbers
Test name
Executions per second
✓
abs and %
1,161,518,208 Ops/sec
Max min
54,267,888 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Overview** MeasureThat.net is a website that allows users to create and run JavaScript microbenchmarks to compare the performance of different approaches in various scenarios. The benchmark provided measures the performance of two methods: using `Math.max` and `Math.min` together, versus using `Math.abs` and calculating the modulus (`%`) separately. **Test Cases** There are two individual test cases: 1. **"Max min"`** This test case uses the following JavaScript code: ```javascript return Math.max(250, Math.min(750, this.number)); ``` The purpose of this test is to measure the performance of combining `Math.max` and `Math.min` to find the maximum value between two numbers. 2. **"abs and %"`** This test case uses the following JavaScript code: ```javascript return Math.abs(this.number % 750); ``` The purpose of this test is to measure the performance of using `Math.abs` to calculate the absolute value of the remainder (`%`) when dividing a number by 750. **Options Compared** In both test cases, the same variable `this.number` is used, which is assigned a random value between 0 and 1000. The difference lies in how the calculations are performed: * "Max min" uses a single call to `Math.max` with two arguments. * "abs and %" uses two separate calls: one to `Math.abs` and another to calculate the modulus (`%`) using the modulo operator (`%`). **Pros and Cons** Here's a brief analysis of the pros and cons of each approach: 1. **"Max min"`** * Pros: + Simplified code. + Fewer function calls, which can be faster due to caching and optimization by JavaScript engines. * Cons: + May involve more work for the JavaScript engine to calculate the maximum value, especially if the inputs are close together. 2. **"abs and %"`** * Pros: + Separate calculations may allow for better cache locality and optimization. * Cons: + More code lines, which can make it harder to read and understand. **Library Usage** There is no external library used in this benchmark. However, the `Math` object is a built-in JavaScript object that provides various mathematical functions, including `max`, `min`, `abs`, and the modulo operator (`%`). **Special JS Features or Syntax** None of the provided code snippet uses any special JavaScript features or syntax. The focus is on comparing the performance of simple arithmetic operations. **Other Alternatives** If you want to compare alternative approaches for finding the maximum value, some examples include: * Using a binary search algorithm. * Comparing two values using `>`, `<`, and logical operators (`&&` and `||`). * Using a different data structure, such as an array or object. Please note that these alternatives may have varying performance characteristics depending on the specific scenario and JavaScript engine used.
Related benchmarks
Math.floor vs alternatives 2
Comments
Confirm delete:
Do you really want to delete benchmark?