Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
map vs object vs switch dispatcher
(version: 1)
Comparing performance of:
mapDispatch vs objectDispatch vs switchDispatch
Created:
4 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
const create = () => console.log("Creating"); const find = () => console.log("Finding"); const index = () => console.log("Indexing"); const update = () => console.log("Updating"); const destroy = () => console.log("Destroying"); const actionNames = { CREATE: "CREATE", FIND: "FIND", INDEX: "INDEX", UPDATE: "UPDATE", DESTROY: "DESTROY", } const actions = new Map([ [actionNames.CREATE, create], [actionNames.FIND, find], [actionNames.INDEX, index], [actionNames.UPDATE, update], [actionNames.DESTROY, destroy], ]); const actionsObject = { [actionNames.CREATE]: create, [actionNames.FIND]: find, [actionNames.INDEX]: index, [actionNames.UPDATE]: update, [actionNames.DESTROY]: destroy, } function mapDispatch({actionName, payload}) { const action = actionsObject[actionName]; if(action === undefined) return; return action(payload); } function objectDispatch({actionName, payload}) { const action = actions.get(actionName); if(action === undefined) return; return action(payload); } function switchDispatch({actionName, payload}) { switch(actionName) { case actionNames.CREATE: return create(payload); case actionNames.FIND: return find(payload); case actionNames.INDEX: return index(payload); case actionNames.UPDATE: return update(payload); case actionNames.DESTROY: return destroy(payload); default: return; } } var randomActions = [actionNames.DESTROY, actionNames.CREATE, actionNames.DESTROY, actionNames.FIND, actionNames.CREATE, actionNames.FIND, actionNames.CREATE, actionNames.UPDATE]
Tests:
mapDispatch
for(const action of randomActions) { mapDispatch({actionName: action, payload: {}}); }
objectDispatch
for(const action of randomActions) { objectDispatch({actionName: action, payload: {}}); }
switchDispatch
for(const action of randomActions) { switchDispatch({actionName: action, payload: {}}); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
mapDispatch
objectDispatch
switchDispatch
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the provided benchmark and explain what's being tested, the pros and cons of different approaches, and other considerations. **Benchmark Overview** The benchmark is designed to compare the performance of three dispatch functions: 1. `mapDispatch` 2. `objectDispatch` 3. `switchDispatch` These functions are used to handle actions with payloads. The benchmark creates an array of random action names (`randomActions`) and executes each action using one of the three dispatch functions. ** dispatchedFunctions** Let's examine each dispatch function: ### 1. mapDispatch `mapDispatch` uses a `Map` data structure to store actions, where each key is an action name, and the value is the corresponding action function. ```javascript function mapDispatch({actionName, payload}) { const action = actionsObject[actionName]; if (action === undefined) return; return action(payload); } ``` Pros: * Fast lookup time for `Map` data structure * Easy to add or remove actions Cons: * May lead to slower performance due to the overhead of creating a new function object on every dispatch * Can be less readable than other approaches, especially for complex logic ### 2. objectDispatch `objectDispatch` uses an object (`actions`) with action names as keys and action functions as values. ```javascript function objectDispatch({actionName, payload}) { const action = actions[actionName]; if (action === undefined) return; return action(payload); } ``` Pros: * Similar to `mapDispatch`, but uses an object instead of a `Map` * May be more readable than `mapDispatch` for some use cases Cons: * Slower lookup time compared to `Map` due to the overhead of property lookups * May lead to slower performance due to the overhead of creating a new function object on every dispatch ### 3. switchDispatch `switchDispatch` uses a `switch` statement with action names as case labels. ```javascript function switchDispatch({actionName, payload}) { switch (actionName) { case actionNames.CREATE: return create(payload); case actionNames.FIND: return find(payload); // ... default: return; } } ``` Pros: * Fast and efficient lookup time due to the `switch` statement * Can be more readable for simple logic Cons: * Limited flexibility compared to `mapDispatch` or `objectDispatch` * May lead to slower performance due to the overhead of evaluating multiple cases **Library:** None **Special JS Features/Syntax:** No special features or syntax are used in this benchmark. However, it's worth noting that modern JavaScript engines and browsers often use various optimizations and caching mechanisms to improve performance. **Other Considerations:** * The benchmark uses a fixed array of random action names (`randomActions`) which may not accurately represent real-world scenarios. * The benchmark executes each action using the same payload object (`{}`) which may not be representative of complex actions that require different payloads. * The benchmark does not consider other factors like caching, memoization, or optimization techniques that might improve performance. **Alternatives:** Other dispatch functions or approaches could be used in this benchmark, such as: * Using a `Reduce` function to accumulate action results * Employing a `forEach` loop to iterate over actions * Utilizing a custom data structure like a trie or a graph * Leveraging a library like Redux or MobX for state management Keep in mind that the choice of dispatch function ultimately depends on the specific requirements and constraints of your application.
Related benchmarks:
edit map vs slice no index
edit map vs slice no index optim
edit map vs slice no index end
edit map vs slice no index start
new Map vs set array to map
Comments
Confirm delete:
Do you really want to delete benchmark?