Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
fast absolute value (branched & branchless) vs abs vs ** -1
(version: 0)
Comparing performance of:
native vs custom (branchless) vs custom (branched) vs -1 ** a
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var abs = Math.abs; var fastAV = a => a - ((a < 0) * a * 2); var fastAV2 = a => a < 0 ? -a : a; var fastAV3 = a => (-1) ** (a > 0) * a;
Tests:
native
abs(-10); abs(10); abs(-10.1); abs(10.1);
custom (branchless)
fastAV(-10); fastAV(10); fastAV(-10.1); fastAV(10.1);
custom (branched)
fastAV2(-10); fastAV2(10); fastAV2(-10.1); fastAV2(10.1);
-1 ** a
fastAV3(-10); fastAV3(10); fastAV3(-10.1); fastAV3(10.1);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
native
custom (branchless)
custom (branched)
-1 ** a
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/17.6 Safari/605.1.15
Browser/OS:
Safari 17 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
native
8572205.0 Ops/sec
custom (branchless)
8792451.0 Ops/sec
custom (branched)
9023908.0 Ops/sec
-1 ** a
8547974.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks on MeasureThat.net. **Benchmark Definition** The benchmark measures the performance of three different approaches to calculate the absolute value (abs) of a number: `Math.abs`, a branchless approach (`fastAV`), and two branched approaches (`fastAV2` and `fastAV3`). The latter two use special JavaScript syntax, `-1 ** a`. **Approaches Compared** The benchmark compares four test cases: 1. **Native**: Uses the built-in `Math.abs` function. 2. **Custom (Branchless)**: Implements an optimized branchless approach using bitwise operations (`fastAV`). 3. **Custom (Branched)**: Uses two different branched approaches with special JavaScript syntax (`fastAV2` and `fastAV3`). **Pros and Cons of Each Approach** 1. **Native**: Pros: * Easy to implement and understand. * Well-optimized by the browser. Cons: * May have performance overhead due to function call overhead. 2. **Custom (Branchless)**: Pros: * Optimized for performance using bitwise operations. * Avoids function call overhead. Cons: * Requires knowledge of bitwise operations and their implications on performance. 3. **Custom (Branched)**: Pros: * Easy to implement and understand, especially for developers familiar with the syntax. Cons: * May have performance overhead due to branching. 4. **-1 ** a**: Pros: * Unconventional approach using special JavaScript syntax. Cons: * May not be easily understandable or maintainable. **Library Used** None explicitly mentioned in the benchmark definition. However, `Math.abs` is a built-in function provided by JavaScript engines, including Safari. **Special JS Feature or Syntax** The `-1 ** a` approach uses a special JavaScript syntax known as "exponentiation" with a negative base and fractional exponent. This syntax is not widely supported and may not work in all environments. **Other Alternatives** If you want to explore more performance-critical optimizations, you can consider using: * SIMD (Single Instruction, Multiple Data) instructions, which are available on modern CPUs. * Just-In-Time (JIT) compilation, which can optimize JavaScript code at runtime. Keep in mind that these alternatives require a good understanding of computer science and programming languages. I hope this explanation helps! Let me know if you have any further questions.
Related benchmarks:
Fast approx. atan2 vs Math.atan2 vs cached atan2 (v8 optimization buster + local scope)
Math.abs speed vs multiply full example
Math.abs speed vs multiply full example vs steps
Math.abs speed vs multiply full example vs steps mid point
Math.abs speed vs multiply vs steps mid point vs epsilon
Comments
Confirm delete:
Do you really want to delete benchmark?