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:
one month ago
by:
Guest
Jump 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:
one month 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
Test name
Executions per second
Switch
420214.6 Ops/sec
Object Literal
376280.3 Ops/sec
If-Else
371942.3 Ops/sec
Related benchmarks:
Switch vs Object Literal
Switch vs Object Literal1551511551
Switch vs Object Literal defined once
Switch vs Object Literal Fixed
Switch vs Object Literal (2)
Object Literal vs Switch
Switch vs Object Literal - testing with stable input
Switch vs Object Literal vs If Else
Switch vs Object Literal version 2
Comments
Confirm delete:
Do you really want to delete benchmark?