Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Math.abs speed vs multiply full example vs steps mid point
(version: 0)
Comparing performance of:
Math.abs(x) vs compare x*x vs use steps vs normilized
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var value = -0.07 var step = 0.01 var min = -5 // sorry we must use var and not let and const function validateStepWithAbs(value, step, min = 0) { if (value == null) return false value = Math.round(value * 1000000) / 1000000 step = Math.round(step * 1000000) / 1000000 min = Math.round(min * 1000000) / 1000000 var absValue = Math.abs(value) var absStep = Math.abs(step) var absMin = Math.abs(min) while ( (absValue > 0 && absValue < 1) || (absStep > 0 && absStep < 1) || (absMin > 0 && absMin < 1) ) { value *= 10 step *= 10 min *= 10 absValue = Math.abs(value) absStep = Math.abs(step) absMin = Math.abs(min) } return (value - min) % step === 0 } function validateStepWithMultiply(value, step, min = 0) { if (value == null) return false value = Math.round(value * 1000000) / 1000000 step = Math.round(step * 1000000) / 1000000 min = Math.round(min * 1000000) / 1000000 while ( (value !== 0 && value * value < 1) || (step !== 0 && step * step < 1) || (min !== 0 && min * min < 1) ) { value *= 10 step *= 10 min *= 10 } return (value - min) % step === 0 } function validateStepWithSteps(value, step, min = 0) { if (value == null) return false var steps = (value - min) / step var remainder = steps % 1 // Since the remainder could be close to either 0.99999999999999 or 0.00000000000001 remainder = Math.round((remainder) * 1000000000) / 1000000000 return remainder === 0 || remainder === 1 } function validateStepWithStepsMid(value, step, min = 0) { if (value == null) return false var steps = (value - min) / step var remainder = steps % 1 // Since the remainder could be close to either 0.99999999999999 or 0.00000000000001 remainder = 0.5 - Math.abs(remainder - 0.5) return remainder < 0.000001 }
Tests:
Math.abs(x)
validateStepWithAbs(value, step, min)
compare x*x
validateStepWithMultiply(value, step, min)
use steps
validateStepWithSteps(value, step, min)
normilized
validateStepWithStepsMid(value, step, min)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Math.abs(x)
compare x*x
use steps
normilized
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll do my best to explain the benchmark and its results. **Benchmark Overview** The benchmark measures the performance of three different approaches for checking whether a value is close to a certain step: `validateStepWithAbs`, `validateStepWithMultiply`, `validateStepWithSteps`, and `validateStepWithStepsMid`. The test cases are designed to check the speed of each approach on a specific input, where the value ranges from -5 to 0. **Approaches Comparison** 1. **`validateStepWithAbs`**: This approach uses the `Math.abs` function to calculate the absolute difference between the value and step. It then enters a loop that multiplies both values by 10 until they are within a certain range, effectively increasing the step size. The loop continues until the difference is exactly divisible by the original step size. * Pros: Simple and straightforward implementation. * Cons: May not be efficient for large step sizes due to repeated divisions and multiplications. 2. **`validateStepWithMultiply`**: This approach uses multiplication instead of division to check if the value is close to the step. It calculates the square root of both values, multiplies them by 10 until they are within a certain range, and then checks if the result is exactly divisible by the original step size. * Pros: Efficient for large step sizes due to optimized multiplication and comparison operations. * Cons: May be less intuitive than `validateStepWithAbs` due to the use of square roots. 3. **`validateStepWithSteps`**: This approach calculates the number of steps required to reach a certain value using the formula `(value - min) / step`. It then checks if the remainder is exactly 0 or 1, indicating that the value is close to the step. * Pros: Efficient and accurate for large step sizes due to optimized division operations. * Cons: May not be suitable for small step sizes due to potential precision issues. 4. **`validateStepWithStepsMid`**: This approach modifies the calculation from `validateStepWithSteps` by adding 0.5 to the remainder, effectively shifting the comparison threshold. * Pros: Provides a more relaxed comparison threshold and can handle smaller step sizes with better accuracy. * Cons: May be slower than other approaches for large step sizes due to additional calculations. **Library Used** The benchmark uses JavaScript's built-in `Math` library, specifically: * `Math.abs`: calculates the absolute value of a number. * `Math.round`: rounds a number to the nearest integer. * `Math.sqrt`: calculates the square root of a number. **Device and OS Information** The benchmark results are from Chrome 115 running on Mac OS X 10.15.7 on a desktop device. **Results** Based on the latest benchmark result, we can see that: 1. **`validateStepWithStepsMid`**: The fastest approach with an average execution rate of approximately 675622 executions per second. 2. **`validateStepWithMultiply`**: Second-fastest approach with an average execution rate of around 3116777 executions per second. 3. **`validateStepWithAbs`**: Third-fastest approach with an average execution rate of about 3170949 executions per second. 4. **`validateStepWithSteps`**: Slowest approach with an average execution rate of approximately 1901867.875 executions per second. In summary, the `validateStepWithStepsMid` approach appears to be the most efficient and accurate method for checking if a value is close to a certain step size, followed closely by `validateStepWithMultiply`.
Related benchmarks:
Math.abs speed vs multiply full example
Math.abs speed vs multiply full example vs steps
Math.abs speed vs multiply vs steps mid point vs epsilon
* 10, round vs trunc vs floor vs parseFloat vs ~~ vs 0 | vs parseInt vs parseInt, 10
Comments
Confirm delete:
Do you really want to delete benchmark?