Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Ternary operator VS Multi-assign VS Function call
(version: 1)
Comparing performance of:
Ternary vs Assign vs Function vs Simple
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
const n = Math.random() * 1000;
Tests:
Ternary
const t1 = n < 750 ? n < 500 ? n < 250 ? 0 : 1 : 2 : 3;
Assign
if ( n < 250 ) { const t2 = 0 } else if ( n < 500 ) { const t2 = 1 } else if ( n < 750 ) { const t2 = 2 } else { const t2 = 3 }
Function
const t3 = (n=>{ if ( n < 250 ) return 0; if ( n < 500 ) return 1; if ( n < 750 ) return 2; return 3; })(n);
Simple
const t4 = parseInt( n % 250 );
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Ternary
Assign
Function
Simple
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (X11; CrOS x86_64 14541.0.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36
Browser/OS:
Chrome 126 on Chrome OS 14541.0.0
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Ternary
43662144.0 Ops/sec
Assign
43712152.0 Ops/sec
Function
48912936.0 Ops/sec
Simple
26153862.0 Ops/sec
Related benchmarks:
Math.max/min vs if vs ternary operator vs custom function
Math.max/min vs if vs ternary operator with random 2
if vs ternary vs multiplication
Math.max/min vs if with const vs ternary operator with const
Math.max/min vs if with const vs ternary operator with const - 2
Clamping via min-max or ternary operator
Logical vs Ternary perf (javascript)
ternary vs if in loop
Functional Vs Imperative
Comments
Confirm delete:
Do you really want to delete benchmark?