Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
switch function vs switch inline vs object vs Map
(version: 1)
Comparing performance of:
switch function vs switch inline vs object vs Map
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
const getSwitch = function (input) { switch(input) { case 0: return 'zero'; case 1: return 'one'; case 2: return 'two'; case 3: return 'three'; case 4: return 'four'; case 5: return 'five'; case 6: return 'six'; case 7: return 'seven'; case 8: return 'eight'; case 9: return 'nine'; case 10: return 'ten'; } } const LUT = { 0: 'zero', 1: 'one', 2: 'two', 3: 'three', 4: 'four', 5: 'five', 6: 'six', 7: 'seven', 8: 'eight', 9: 'nine', 10: 'ten' } const map = new Map([ [0, 'zero'], [1, 'one'], [2, 'two'], [3, 'three'], [4, 'four'], [5, 'five'], [6, 'six'], [7, 'seven'], [8, 'eight'], [9, 'nine'], [10, 'ten'] ]);
Tests:
switch function
const result = getSwitch(Math.floor(Math.random() * 10));
switch inline
let result; switch(Math.floor(Math.random() * 10)) { case 0: result = 'zero'; break; case 1: result = 'one'; break; case 2: result = 'two'; break; case 3: result = 'three'; break; case 4: result = 'four'; break; case 5: result = 'five'; break; case 6: result = 'six'; break; case 7: result = 'seven'; break; case 8: result = 'eight'; break; case 9: result = 'nine'; break; case 10: result = 'ten'; break; }
object
const result = LUT[Math.floor(Math.random() * 10)];
Map
const result = map.get(Math.floor(Math.random() * 10));
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
switch function
switch inline
object
Map
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64; rv:132.0) Gecko/20100101 Firefox/132.0
Browser/OS:
Firefox 132 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
switch function
20525988.0 Ops/sec
switch inline
32947854.0 Ops/sec
object
164263936.0 Ops/sec
Map
696350016.0 Ops/sec
Related benchmarks:
Switch vs map
Switch vs map with string keys
Switch vs map with string keys and early exit
Switch vs map with string keys and early exit 2
switch vs if-else vs lookup
Map Vs Switch
Switch vs new Map
Switch vs Map object strings
Switch vs map test2
Comments
Confirm delete:
Do you really want to delete benchmark?