Toggle navigation
MeasureThat
.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Run results for:
Switch vs Object Literal vs If-Else
Go to the benchmark
Embed
Embed Benchmark Result
Run details:
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/146.0.0.0 Safari/537.36
Browser:
Chrome 146
Operating system:
Linux
Device Platform:
Desktop
Date tested:
4 months ago
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
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'); }