Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Switch case vs Map object
(version: 0)
Comparing performance of:
Switch vs Map object
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var str = 'abcdefghijklmno'; str = str.charAt(Math.floor(Math.random() * 15));
Tests:
Switch
switch (str) { case 'a': console.log('A'); break; case 'b': console.log('B'); break; case 'c': console.log('C'); break; case 'd': console.log('D'); break; case 'e': console.log('E'); break; case 'f': console.log('F'); break; case 'g': console.log('G'); break; case 'h': console.log('H'); break; case 'i': console.log('I'); break; case 'j': console.log('J'); break; case 'k': console.log('K'); break; case 'l': console.log('L'); break; case 'm': console.log('M'); break; case 'n': console.log('N'); break; case 'o': console.log('O'); break; }
Map object
var map = { a: console.log('A'), b: console.log('B'), c: console.log('C'), d: console.log('D'), e: console.log('E'), f: console.log('F'), g: console.log('G'), h: console.log('H'), i: console.log('I'), j: console.log('J'), k: console.log('K'), l: console.log('L'), m: console.log('M'), n: console.log('N'), o: console.log('O'), }; map[str];
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Switch
Map object
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
12 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
Browser/OS:
Chrome 147 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Switch
11319.0 Ops/sec
Map object
902.2 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Overview of the Benchmark** The provided JSON represents a JavaScript microbenchmark test case on MeasureThat.net. The benchmark compares the performance of two approaches: switch-case statements and Map objects. **Switch-Case Statements** In the `Benchmark Definition` for "Switch", the script uses a traditional switch-case statement to check if the input string `str` matches any of the predefined cases. If a match is found, it logs a message to the console using `console.log`. The code is identical to what is written in the `Script Preparation Code`. **Map Objects** In the `Benchmark Definition` for "Map object", an object literal is created with keys and corresponding values. The input string `str` is then used as a key to access the corresponding value from the map. If the key is found, it logs a message to the console using `console.log`. The code is similar to what is written in the `Script Preparation Code`. **Options Compared** The two options compared are: 1. Traditional switch-case statements 2. Map objects **Pros and Cons** **Switch-Case Statements:** Pros: * Well-established and widely supported in JavaScript engines. * Can be more readable for small, fixed cases. Cons: * Can lead to longer execution times due to the overhead of the interpreter trying to find a match. * May not perform well with large numbers of cases or dynamic case values. **Map Objects:** Pros: * Fast and efficient lookup times. * Can handle arbitrary key-value pairs. * Modern JavaScript engines tend to optimize Map operations. Cons: * Requires object creation, which can incur overhead. * May have higher memory usage compared to switch-case statements. **Library Used** In this benchmark, the `console.log` function is used as part of the test. The `console.log` function is a built-in JavaScript function that outputs text to the console. It does not require any external libraries or dependencies. **Special JS Feature/Syntax** There are no special JavaScript features or syntax used in this benchmark. However, it's worth noting that modern JavaScript engines have various optimizations and features that can affect performance, such as: * `let` and `const` declarations (instead of `var`) * Arrow functions * Spread operators * Object destructuring **Other Alternatives** If you want to explore alternative approaches for this benchmark, you could consider using other data structures or algorithms, such as: 1. Regular expressions: Could potentially provide a more concise way to match strings. 2. Arrays and indexing: Could be used to store cases in an array and then iterate through it. 3. Object properties access: Could be used instead of Map objects for fast property access. However, keep in mind that these alternatives might not necessarily offer better performance than the existing switch-case statements or Map objects, depending on the specific use case and implementation details.
Related benchmarks:
Switch vs Object Literal - SR Test
Switch vs Object Literal value return
Switch vs Object Literal bbg
Switch vs Object Literal extended
Comments
Confirm delete:
Do you really want to delete benchmark?