Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Run results for:
if else vs switch case vs JSON
Compares if/else, switch/case and a JSON mapping to functions.
Go to the benchmark
Embed
Embed Benchmark Result
Run details:
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36
Browser:
Chrome 128
Operating system:
Windows
Device Platform:
Desktop
Date tested:
one year ago
Test name
Executions per second
if/else
1003.5 Ops/sec
switch/case
968.2 Ops/sec
JSON
1010.4 Ops/sec
Script Preparation code:
var array = [1, 3, 2]; for(let i = 5; i > 0; i--) { var array = array.concat(array); }; var mappingJSON = { 1: function() { return 'one'; }, 2: function() { return 'two'; }, 3: function() { return 'three'; } };
Tests:
if/else
for(let i = array.length - 1; i >= 0; i--) { let item = array[i]; if(item === 1) { console.log('one'); } else if(item === 2) { console.log('two'); } else if(item === 3) { console.log('three'); }; };
switch/case
for(let i = array.length - 1; i >= 0; i--) { let item = array[i]; switch(item) { case 1: console.log('one'); break; case 2: console.log('two'); break; case 3: console.log('three'); break; }; };
JSON
for(let i = array.length - 1; i >= 0; i--) { let item = array[i]; console.log(mappingJSON[item]()); };