Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Run results for:
if-else vs switch-case vs object literals vs ternary-operator 3
Go to the benchmark
Embed
Embed Benchmark Result
Run details:
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:133.0) Gecko/20100101 Firefox/133.0
Browser:
Firefox 133
Operating system:
Windows
Device Platform:
Desktop
Date tested:
one year ago
Test name
Executions per second
If - Else
49544328.0 Ops/sec
Switch - Case
51068504.0 Ops/sec
Object literals
367578304.0 Ops/sec
ternary-operator
48855652.0 Ops/sec
Script Preparation code:
var str = 'abc'; str = str.charAt(Math.floor(Math.random() * 3));
Tests:
If - Else
let x; if(str === 'a'){ x = 'A'; } else if (str === 'b'){ x = 'B'; } else if (str === 'c'){ x = 'C'; }
Switch - Case
let x; switch (str) { case 'a': x = 'A'; break; case 'b': x = 'B'; break; case 'c': x = 'C'; break; }
Object literals
var objLiteral = { a: 'A', b: 'B', c: 'C', } let x = objLiteral[str];
ternary-operator
let x = str === 'a' ? 'A' : str === 'b' ? 'B' : str === 'c' ? 'C' : 'nothing'