Toggle navigation
MeasureThat
.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Switch vs Object Literal vs If-Else
(version: 1)
Comparing performance of:
Switch vs Object Literal vs If-Else
Created:
4 months ago
by:
Guest
Go to the latest result
Script Preparation code:
var str = 'abc'; str = str.charAt(Math.floor(Math.random() * 3));
Tests:
Switch
switch (str) { case 'a': console.log('A'); break; case 'b': console.log('B'); break; case 'c': console.log('C'); break; }
Object Literal
var objLiteral = { a: function() { console.log('A'); }, b: function() { console.log('B'); }, c: function() { console.log('C'); } } objLiteral[str]();
If-Else
if (str === 'a') { console.log('A'); } else if (str === 'b') { console.log('B'); } else if (str === 'c') { console.log('C'); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Switch
Object Literal
If-Else
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
4 months ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/146.0.0.0 Safari/537.36
Browser/OS:
Chrome 146 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Switch
420K/s
Object Literal
376K/s
If-Else
372K/s
View exact numbers
Test name
Executions per second
✓
Switch
420,215 Ops/sec
Object Literal
376,280 Ops/sec
If-Else
371,942 Ops/sec
Related benchmarks
Switch vs Object Literal
Comments
Confirm delete:
Do you really want to delete benchmark?