Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Edge wrapping - ternary vs magic constant
(version: 0)
Comparing performance of:
Ternary vs Magic vs Magic Scoped
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var wrap_ternary = function(x) { return x < 0 ? x + 1 : x > 1 ? x - 1 : x; } var wrap_magic = function(x) { return x - Math.floor(x * 0.99999994); } const floor = Math.floor; var wrap_magic_scope = function(x) { return x - floor(x * 0.99999994); }
Tests:
Ternary
wrap_ternary(-1.00); wrap_ternary(-0.75); wrap_ternary(-0.50); wrap_ternary(-0.25); wrap_ternary( 0.00); wrap_ternary( 0.25); wrap_ternary( 0.50); wrap_ternary( 0.75); wrap_ternary( 1.00); wrap_ternary( 1.25); wrap_ternary( 1.50); wrap_ternary( 1.75); wrap_ternary( 2.00);
Magic
wrap_magic(-1.00); wrap_magic(-0.75); wrap_magic(-0.50); wrap_magic(-0.25); wrap_magic( 0.00); wrap_magic( 0.25); wrap_magic( 0.50); wrap_magic( 0.75); wrap_magic( 1.00); wrap_magic( 1.25); wrap_magic( 1.50); wrap_magic( 1.75); wrap_magic( 2.00);
Magic Scoped
wrap_magic_scope(-1.00); wrap_magic_scope(-0.75); wrap_magic_scope(-0.50); wrap_magic_scope(-0.25); wrap_magic_scope( 0.00); wrap_magic_scope( 0.25); wrap_magic_scope( 0.50); wrap_magic_scope( 0.75); wrap_magic_scope( 1.00); wrap_magic_scope( 1.25); wrap_magic_scope( 1.50); wrap_magic_scope( 1.75); wrap_magic_scope( 2.00);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Ternary
Magic
Magic Scoped
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):
Let's break down the benchmark and its test cases. **Benchmark Definition** The benchmark is designed to measure the performance of JavaScript functions that implement edge wrapping, which is used to limit a value to a specific range. There are three variants: 1. **Ternary**: The function uses a ternary operator (`?:`) to check if the input `x` is less than 0, greater than 1, or within the desired range (0 to 1). If `x` is outside this range, it adds/subtracts 1 from/to `x`. 2. **Magic**: The function uses a magic constant (0.99999994) to check if `x` is less than or greater than 0.99999994. This value is chosen such that `x` will be rounded down when multiplied by this constant and then subtracted from the original value. 3. **Magic Scoped**: This function is similar to the "Magic" variant, but its scope includes a reference to the `Math.floor()` function, which is used to round down the input `x`. **Options Compared** The three test cases compare the performance of these three edge wrapping variants: * Ternary vs Magic: The performance difference between using a ternary operator and a magic constant. * Magic vs Magic Scoped: The impact of including the `Math.floor()` function on the performance of the "Magic" variant. **Pros and Cons** Here are some pros and cons for each approach: 1. **Ternary**: * Pros: Simple to implement, easy to read. * Cons: May be slower due to the overhead of evaluating a ternary operator. 2. **Magic**: * Pros: Often used in real-world code, as it provides a reasonable approximation for rounding down values. * Cons: May not be as readable or maintainable as other approaches, and its performance may vary depending on the specific use case. 3. **Magic Scoped**: * Pros: Includes a reference to `Math.floor()`, which can provide better accuracy for certain use cases. * Cons: May add unnecessary complexity to the code, and its performance may be slightly slower than the "Magic" variant. **Library Usage** The benchmark uses the following library: * `Math`: The JavaScript math library provides various mathematical functions, including `floor()`, which is used in the "Magic Scoped" variant. * `console.log()`: Not explicitly mentioned in the code, but implied as it's often used for logging results or debugging. **Special JS Features** None of the test cases use special JavaScript features or syntax. They are relatively simple and straightforward implementations of edge wrapping functions. **Alternatives** Other alternatives for implementing edge wrapping could include: * Using bitwise operations (e.g., `x & 0xFF` to get the last byte of a float) instead of arithmetic. * Implementing a custom rounding function using multiple conditions instead of relying on built-in library functions. * Using SIMD instructions (if available on the target hardware) to accelerate certain rounding operations. Keep in mind that these alternatives might have different performance characteristics and trade-offs, and their suitability depends on the specific use case and requirements.
Related benchmarks:
toFixed vs toPrecision vs Math.round() vs Math.floorfast vs MDN round_to_precision
toFixed vs toPrecision vs Math.round() vs Math.floorfaster test
toFixed vs toPrecision vs Math.round() vs Math.floorfast vs new Math.trunc vs numeraljs
toFixed vs toPrecision vs Math.round() to 1 decimal place
toFixed() vs String(Math.floor()
Comments
Confirm delete:
Do you really want to delete benchmark?