Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Switch/case vs indexOf
(version: 0)
Comparing performance of:
switch/case vs Dictionary
Created:
one year ago
by:
Guest
Jump to the latest result
Tests:
switch/case
const elementAction = { a: 1, b: 2, c: 3, }; function func() { switch (true) { case Object.hasOwn(elementAction, 'foo'): return false; case Object.hasOwn(elementAction, 'bar'): return false; case Object.hasOwn(elementAction, 'baz'): return false; case Object.hasOwn(elementAction, 'bazz'): return false; case Object.hasOwn(elementAction, 'fooz'): return false; case Object.hasOwn(elementAction, 'foob'): return false; case Object.hasOwn(elementAction, 'doob'): return false; case Object.hasOwn(elementAction, 'bafz'): return false; case Object.hasOwn(elementAction, 'barz'): return false; case Object.hasOwn(elementAction, 'gas'): return false; case Object.hasOwn(elementAction, 'gaz'): return false; case Object.hasOwn(elementAction, 'daz'): return false; case Object.hasOwn(elementAction, 'dar'): return false; case Object.hasOwn(elementAction, 'har'): return false; case Object.hasOwn(elementAction, 'ooph'): return false; case Object.hasOwn(elementAction, 'phooph'): return false; default: return true; } } func();
Dictionary
const elementAction = { a: 1, b: 2, c: 3, }; const dictionary = [ { key: 'foo', value: () => false, }, { key: 'bar', value: () => false, }, { key: 'baz', value: () => false, }, { key: 'bazz', value: false, }, { key: 'fooz', value: false, }, { key: 'doob', value: false, }, { key: 'bafz', value: false, }, { key: 'barz', value: false, }, { key: 'gas', value: false, }, { key: 'gaz', value: false, }, { key: 'daz', value: false, }, { key: 'dar', value: false, }, { key: 'har', value: false, }, { key: 'ooph', value: false, }, { key: 'phooph', value: false, }, { key: 'foo', value: false, }, ]; function func() { const index = dictionary.indexOf(({ key }) => Object.hasOwn(elementAction, key)); return index > -1 ? dictionary[index].value : true; } func();
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
switch/case
Dictionary
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36
Browser/OS:
Chrome 131 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
switch/case
725270.8 Ops/sec
Dictionary
11907991.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Benchmark Overview** The benchmark compares the performance of two approaches: `switch/case` and using an array (dictionary) with `indexOf`. **Switch/Case Approach** In this approach, we use a `switch` statement to check if a property exists in the `elementAction` object. The `case` statements have varying conditions, which are unlikely to be met, making it effectively equivalent to checking each property individually. * **Pros:** This approach is easy to understand and implement for developers familiar with `switch/case`. It can be more efficient if the number of properties in `elementAction` is large. * **Cons:** The performance may degrade due to the exhaustive search involved. Additionally, this approach requires defining each possible property value, which can become cumbersome as the number of properties increases. **Dictionary Approach** In this approach, we use an array (`dictionary`) with objects containing a key and a value function. We then use `indexOf` to find the index of the first object in the dictionary where the key exists. * **Pros:** This approach is concise and easy to maintain if new properties are added to `elementAction`. It can be more efficient for large numbers of properties since only the relevant objects need to be checked. * **Cons:** The performance may degrade due to the overhead of searching an array. Additionally, this approach requires defining a value function for each property, which can become complex as the number of properties increases. **Comparison and Considerations** When choosing between these approaches, consider the following: 1. **Performance**: If you anticipate dealing with a large number of properties in `elementAction`, the dictionary approach might be more efficient. 2. **Code Maintainability**: If new properties are frequently added to `elementAction`, the dictionary approach is easier to maintain. **Other Alternatives** There are other approaches that can be used to check if a property exists in an object: * **`in` operator**: This operator checks if a property exists directly in the object. It might offer better performance than exhaustive searches. * **Property access with `hasOwnProperty`**: While not as concise as using `in`, this approach also checks for property existence. However, these alternatives may be less familiar or more complex to implement than the switch/case and dictionary approaches.
Related benchmarks:
indexOf vs findIndex with a simple case
JavaScript search() vs indexOf()
String.indexOf vs String.indexOf with the second parameter
String.indexOf(char) vs String.indexOf(char, position)
Comments
Confirm delete:
Do you really want to delete benchmark?