Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
map vs if/else vs switch
(version: 0)
Comparing performance of:
map lookup vs switch case vs if/else
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var map = new Map([ [0, "Initializing"], [1, "Idle"], [2, "Running"], [3, "Stop"], [4, "Error"], ]); function switchCase(value) { switch (value) { case 0: return "Initializing"; case 1: return "Idle"; case 2: return "Running"; case 3: return "Stop"; case 4: return "Error"; default: return void 0; } } function ifElse(value) { if (value === 0) { return "Initializing"; } else if (value === 1) { return "Idle"; } else if (value === 2) { return "Running"; } else if (value === 3) { return "Stop"; } else if (value === 4) { return "Error"; } }
Tests:
map lookup
let value = map.get(0); value = map.get(32); value = map.get(1); value = map.get(33); value = map.get(2); value = map.get(34); value = map.get(3); value = map.get(35); value = map.get(4);
switch case
let value = switchCase(0); value = switchCase(32); value = switchCase(1); value = switchCase(33); value = switchCase(2); value = switchCase(34); value = switchCase(3); value = switchCase(35); value = switchCase(4);
if/else
let value = ifElse(0); value = ifElse(32); value = ifElse(1); value = ifElse(33); value = ifElse(2); value = ifElse(34); value = ifElse(3); value = ifElse(35); value = ifElse(4);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
map lookup
switch case
if/else
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
19 days ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/147.0.0.0 Safari/537.36 Edg/147.0.0.0
Browser/OS:
Chrome 147 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
map lookup
99951200.0 Ops/sec
switch case
112182584.0 Ops/sec
if/else
100969816.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the benchmark definition and test cases. **Benchmark Definition** The benchmark tests three approaches: `map` lookup, `switch case`, and `if/else`. The script preparation code defines: * A `Map` data structure called `map` with six key-value pairs. * Two functions: + `switchCase`: takes an integer value as input and returns a string based on the value using a `switch` statement. + `ifElse`: takes an integer value as input and returns a string based on the value using multiple `if-else` statements. The benchmark definition is written in JavaScript, which is the language used to execute the test cases. **Options Compared** The three approaches are compared in terms of their performance. Specifically, the benchmark measures: * The number of executions per second (in "ExecutionsPerSecond") for each approach. * The browser and device platform information (in "Browser" and "DevicePlatform", respectively) to understand the test environment. **Pros and Cons** Here's a brief summary of the pros and cons of each approach: * `map` lookup: + Pros: Efficient, fast, and concise way to perform lookups in a data structure. + Cons: May not be suitable for complex or dynamic lookups, as it relies on key existence checks. * `switch case`: + Pros: Easy to read and maintain, especially for simple cases. Can be more efficient than `if-else` chains for certain use cases. + Cons: Limited to exact match logic; can become cumbersome for complex or dynamic logic. * `if-else` chain: + Pros: Flexible and versatile, suitable for most use cases. Easy to add new conditions. + Cons: Can be slower than `map` lookup or `switch case`, especially for large numbers of conditions. **Library and Syntax** The test cases do not require any external libraries beyond JavaScript itself. The syntax used is standard JavaScript. **Special JS Features** None mentioned in the provided benchmark definition. **Alternatives** For comparison purposes, other approaches to achieve similar results could include: * `for` loop with indexing: Instead of using a `map`, you could use an array and iterate through its indices. * Regular expressions (regex): If the data structure is more complex or dynamic, regex might be used to match patterns against input values. * Object-based lookup: Using objects instead of maps for key-value pairs. Keep in mind that these alternatives would likely have different performance characteristics compared to the original `map`-based approach.
Related benchmarks:
Map vs switch soh
JS switch vs if/else if
map vs ifelse vs switch test
Switch vs Object Literal vs If Else vs Map - testing with simpler data again again
Comments
Confirm delete:
Do you really want to delete benchmark?