Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Run results for:
switch function vs switch inline vs object vs Map
Go to the benchmark
Embed
Embed Benchmark Result
Run details:
User agent:
Mozilla/5.0 (X11; Linux x86_64; rv:132.0) Gecko/20100101 Firefox/132.0
Browser:
Firefox 132
Operating system:
Linux
Device Platform:
Desktop
Date tested:
one year ago
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
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));