Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Run results for:
Math.max/min vs if vs ternary vs bitwise & ~~ - 4 numbers
Go to the benchmark
Embed
Embed Benchmark Result
Run details:
User agent:
Mozilla/5.0 (iPhone; CPU iPhone OS 18_7 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.7.2 Mobile/15E148 Safari/604.1
Browser:
Mobile Safari 18
Operating system:
iOS 18.7
Device Platform:
Mobile
Date tested:
one month ago
Test name
Executions per second
Math.max/min
83176136.0 Ops/sec
if
217075328.0 Ops/sec
ternary
223229552.0 Ops/sec
bitwise
227199088.0 Ops/sec
& ~~
213354176.0 Ops/sec
Script Preparation code:
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)); }
Tests:
Math.max/min
Math.min(Math.max(x + clientX - px, 0), maxWidth) - x
if
if(x + clientX - px < 0) return 0; if(x + clientX - px > 750) return maxWidth; return x + clientX - px;
ternary
return x + clientX - px < 0 ? 0 : (x + clientX - px > maxWidth ? maxWidth : x + clientX - px) - x;
bitwise
MIN_INT(MAX_INT(x + clientX - px, 0), maxWidth)
& ~~
MIN(MAX(x + clientX - px, 0), maxWidth)