Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
map vs if vs switch
(version: 1)
Comparing performance of:
map lookup vs switch case vs if/else
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
var map = new Map([ [0, "Initializing"], [1, "Idle"], [2, "Running"] ]); function switchCase(value) { switch (value) { case 0: return "Initializing"; case 1: return "Idle"; default: return "Running" } } function ifElse(value) { if (value === 0) { return "Initializing"; } if (value === 1) { return "Idle"; } return "Running" }
Tests:
map lookup
let value = map.get(0); value = map.get(32); value = map.get(1);
switch case
let value = switchCase(0); value = switchCase(32); value = switchCase(1);
if/else
let value = ifElse(0); value = ifElse(32); value = ifElse(1);
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:
20 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
98133840.0 Ops/sec
switch case
91938680.0 Ops/sec
if/else
87864888.0 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The benchmark defined in the JSON compares the performance of three different approaches to conditional logic and value retrieval in JavaScript. It specifically evaluates: 1. **Using a Map for lookups** 2. **Using a switch-case statement** 3. **Using if-else statements** ### Description of Approaches 1. **Map Lookup**: - The benchmark initializes a `Map` with predefined key-value pairs. The key represents input values (0, 1, 2), and the value is a string associated with each key. The test retrieves values from the map using the `get` method. - **Pros**: - Quick access and lookup time (thanks to hash table properties). - Offers an elegant syntax for associating keys with values. - **Cons**: - Slight overhead when creating the `Map`, especially for small sets of known values due to the additional memory footprint. - Less intuitive for those unfamiliar with JavaScript's `Map` object. 2. **Switch-Case Statement**: - The `switchCase` function uses a classic switch-case structure to evaluate a given input and return corresponding strings. - **Pros**: - Clear and readable for cases with multiple discrete options. - Possible efficiencies via "jump tables" in some JavaScript engines, particularly for dense integer distributions. - **Cons**: - Performance can degrade with an increasing number of cases or spread-out values, resulting in more checks. - Slightly more verbose than some alternatives. 3. **If-Else Statements**: - The `ifElse` function sequentially checks conditions using if-else statements. - **Pros**: - Straightforward logic flow; easy to understand for many developers. - Ideal for situations with infrequent evaluations, but with clear branching requirements. - **Cons**: - Can become unwieldy and less readable with increased complexity. - Performance can deteriorate with many sequential checks, particularly for deeper nesting compared to a switch-case or Maps. ### Benchmark Results The benchmark results indicated the executions per second for each of the methods: - **Switch-Case**: 176,699,104 executions per second (fastest) - **Map Lookup**: 166,121,440 executions per second - **If-Else**: 149,535,904 executions per second (slowest) ### Considerations & Alternatives - **Performance**: The performance advantages noted in the benchmark may vary based on the JavaScript engine, input distribution, and overall workload. The `switch-case` proved to be the fastest for these test case scenarios. - **Readability and Maintainability**: When choosing between these approaches, the specific context and team familiarity should guide decisions. While performance is critical, code readability and maintainability in a team environment are equally important. - **Alternatives**: Other alternatives to consider include: - **Object Literals**: Using plain object mappings for similar use-cases where keys are known and dense. This can sometimes be even faster than using a Map due to less overhead in some cases. - **Function Lookup Tables**: Instead of using control structures, a function reference can be invoked directly based on the index, which can further enhance performance in specific contexts. In sum, the stated approaches provide various benefits depending on the use case, with considerations to balance between speed, clarity, and complexity. Choosing the correct one often depends on the specific requirements of your application and the environment in which it operates.
Related benchmarks:
Map Vs Switch
Switch true vs if else
switch vs if js
JS switch vs if/else if
map vs if/else vs switch
Conditional check
if/switch1
if-else vs switch4
if else vs switch case vs indexOf 2
Comments
Confirm delete:
Do you really want to delete benchmark?