Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Switch vs Functional Approach
(version: 0)
Comparing performance of:
Vanilla Switch vs Switch Fn style
Created:
2 years ago
by:
Guest
Jump to the latest result
Tests:
Vanilla Switch
function isFunc (v) { return typeof v === 'function'; } function isDefined (v) { return v !== undefined && v !== null; } function switchEnum(e, handlers) { const specific = handlers[e]; if (isDefined(specific)) { return isFunc(specific) ? specific(e) : specific; } if (isDefined(handlers.else)) { return isFunc(handlers.else) ? handlers.else(e) : handlers.else; } throw new Error(`Unhandled switchEnum statement for value (${e}).`); } function normal(inp) { switch (inp) { case "today": return 1; case "tomorrow": return 2; case "yesterday": return -1; default: return 0; } }; normal('today'); normal('yesterday'); normal('tomorrow');
Switch Fn style
function isFunc (v) { return typeof v === 'function'; } function isDefined (v) { return v !== undefined && v !== null; } function switchEnum(e, handlers) { const specific = handlers[e]; if (isDefined(specific)) { return isFunc(specific) ? specific(e) : specific; } if (isDefined(handlers.else)) { return isFunc(handlers.else) ? handlers.else(e) : handlers.else; } throw new Error(`Unhandled switchEnum statement for value (${e}).`); } function funcccy(inp) { return switchEnum(inp, { "today": () => 1, "tomorrow": () => 2, "yesterday": () => -1, "else": () => 0, }); } funcccy('today'); funcccy('yesterday'); funcccy('tomorrow');
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Vanilla Switch
Switch Fn style
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:125.0) Gecko/20100101 Firefox/125.0
Browser/OS:
Firefox 125 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Vanilla Switch
63907816.0 Ops/sec
Switch Fn style
8259009.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Benchmark Explanation** MeasureThat.net is a website that allows users to create and run JavaScript microbenchmarks. The provided benchmark compares two approaches: the traditional `switch` statement and a functional programming style using a callback function. **Traditional Switch Statement (Vanilla Switch)** The traditional switch statement is compared to its functional equivalent. In this case, both test cases use the same code structure: 1. Define a set of handlers with values as keys and functions as values. 2. Use the `switch` statement to select the corresponding handler based on the input value. 3. Call the selected function with the input value as an argument. In the vanilla switch test case, the `normal` function is called with different input values: `"today"`, `"yesterday"`, and `"tomorrow"`. **Functional Approach (Switch Fn style)** The functional approach uses a callback function to achieve the same result: 1. Define a set of handlers with values as keys and functions as values. 2. Use a closure to capture the handlers object and create a new function `funcccy` that takes an input value. 3. Within `funcccy`, call the `switchEnum` function with the input value and the handlers object. In this test case, the `funcccy` function is called with different input values: `"today"`, `"yesterday"`, and `"tomorrow"`. **Comparison** The comparison between these two approaches aims to measure which one performs better in terms of execution speed. The results show that the functional approach (`Switch Fn style`) outperforms the traditional switch statement (`Vanilla Switch`). **Pros and Cons** **Traditional Switch Statement (Vanilla Switch)** Pros: * Easier to understand and maintain for developers familiar with the syntax. * Can be more efficient in certain cases, such as when using a large number of handlers. Cons: * Tightly coupled with the specific handler names and values. * May require more memory allocation due to the use of a `switch` statement. **Functional Approach (Switch Fn style)** Pros: * More flexible and reusable, as it can be applied to various scenarios without modifying the underlying code. * Can lead to better maintainability and scalability in larger applications. Cons: * Requires a deeper understanding of functional programming concepts and may be less intuitive for developers unfamiliar with this paradigm. * May introduce additional overhead due to the creation of new functions and closures. **Other Considerations** Both approaches have their trade-offs, and the choice between them depends on the specific use case, team preferences, and performance requirements. In general, the functional approach can provide better maintainability and scalability, but may require more expertise in functional programming concepts. **Library: `isFunc` and `.isDefined`** These functions are used to check if a value is a function or defined, respectively. They are likely utility functions used within the benchmark for testing purposes. **Special JS Feature/Syntax: `switchEnum`** This feature is used to define a set of handlers with values as keys and functions as values. It's an extension to the traditional `switch` statement that allows for more flexibility in handling different cases. Note that `switchEnum` is not a standard JavaScript feature, but rather a custom implementation within this benchmark.
Related benchmarks:
Map vs switch soh
map vs ifelse vs switch test
Switch vs Functional Approach 3
Switch/case vs indexOf
Comments
Confirm delete:
Do you really want to delete benchmark?