<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.7.11/lodash.min.js'></script>
var x = Math.random() * 1000;
var clientX = Math.random() * 1000;
var px = Math.random() * 1000;
var maxWidth = Math.random() * 1000;
function MAX_INT(a, b) {
return a - ((a - b) & ((a - b) >> 31));
}
function MIN_INT(a, b) {
return a - ((a - b) & ((b - a) >> 31));
}
function MAX(a, b) {
const intA = ~~a;
const intB = ~~b;
return intA - ((intA - intB) & ((intA - intB) >> 31));
}
function MIN(a, b) {
const intA = ~~a;
const intB = ~~b;
return intA - ((intA - intB) & ((intB - intA) >> 31));
}
Math.min(Math.max(x + clientX - px, 0), maxWidth) - x
if(x + clientX - px < 0) return 0;
if(x + clientX - px > 750) return maxWidth;
return x + clientX - px;
return x + clientX - px < 0
? 0
: (x + clientX - px > maxWidth
? maxWidth
: x + clientX - px) - x;
MIN_INT(MAX_INT(x + clientX - px, 0), maxWidth)
MIN(MAX(x + clientX - px, 0), maxWidth)
_.clamp(x + clientX - px, 0, maxWidth)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Math.max/min | |
if | |
ternary | |
bitwise | |
& ~~ | |
lodash clamp |
Test name | Executions per second |
---|---|
Math.max/min | 2566094.8 Ops/sec |
if | 7606500.5 Ops/sec |
ternary | 7805290.5 Ops/sec |
bitwise | 3752870.0 Ops/sec |
& ~~ | 3732540.8 Ops/sec |
lodash clamp | 3431596.0 Ops/sec |
Let's break down the provided benchmark JSON and explain what is tested, compared, and their pros and cons.
Benchmark Definition
The test case defines six different approaches to compare:
Math.min(Math.max(x + clientX - px, 0), maxWidth)
- This approach uses built-in JavaScript functions to achieve a similar result.if (x + clientX - px < 0) return 0; if (x + clientX - px > 750) return maxWidth; return x + clientX - px;
- This approach uses an if-else
statement with conditional returns.return x + clientX - px < 0 ? 0 : (x + clientX - px > maxWidth ? maxWidth : x + clientX - px) - x;
- This approach uses a ternary operator with multiple conditions.MIN_INT(MAX_INT(x + clientX - px, 0), maxWidth)
- This approach uses bitwise operations to achieve a similar result, using two custom functions: MAX_INT
and MIN_INT
.MIN(MAX(x + clientX - px, 0), maxWidth)
- This approach uses bitwise operations with a single function, MIN
, and the ~~
operator._.clamp(x + clientX - px, 0, maxWidth)
- This approach uses the Lodash library's clamp
function to achieve a similar result.Comparison
Each test case compares the execution time of one approach against another, allowing users to see which method is faster in certain scenarios.
Pros and Cons
Other Considerations
~~
operator is used in some approaches, which is a bitwise NOT operator that converts numbers to integers. This can be an interesting edge case to explore, especially for those familiar with bitwise operations.MAX_INT
and MIN_INT
) in the bitwise approach adds complexity but may provide performance benefits.Overall, this benchmark allows users to compare different approaches to achieving a similar result, helping them understand which methods are faster and more efficient in various scenarios.