Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Switch vs Functional Approach 3
(version: 0)
Comparing performance of:
Vanilla Switch vs Switch Fn style
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
const use = (a, fn) => fn(a); const isFunc = (v) => typeof v === 'function'; const isDefined = v => v !== undefined && v !== null; const switchEnum = (e, handlers) => use(handlers[e] || handlers.else, fn => isFunc(fn) ? fn(e) : e); function normal(inp) { switch (inp) { case "today": return 1; case "tomorrow": return 2; case "yesterday": return -1; default: return 0; } }; function funcccy(inp) { return switchEnum(inp, { "today": () => 1, "tomorrow": () => 2, "yesterday": () => -1, "else": () => 0, }); }
Tests:
Vanilla Switch
normal('today'); normal('yesterday'); normal('tomorrow');
Switch Fn style
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 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36
Browser/OS:
Chrome 122 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Vanilla Switch
10824798.0 Ops/sec
Switch Fn style
6031819.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested. **Overview** The benchmark compares two approaches to handling switch statements in JavaScript: traditional vanilla switch and a functional approach using a callback function (`switchEnum`). **Vanilla Switch Approach** In this approach, the `normal` function uses a traditional switch statement with multiple cases. The `switch` keyword is used to determine which case to execute based on the input value. **Functional Approach (Switch Fn style)** The `funcccy` function uses a functional approach by wrapping the original `normal` function in an additional layer using the `use` and `isFunc` functions. This allows the callback function (`switchEnum`) to be applied to the result of the switch statement, effectively turning it into a function that can be executed directly. **Pros and Cons** **Vanilla Switch Approach:** Pros: * Easy to read and understand * Well-supported by most JavaScript engines Cons: * Can lead to slower execution times due to the overhead of the `switch` keyword * May not perform as well with large switch statements or complex logic **Functional Approach (Switch Fn style):** Pros: * Can provide better performance due to the reduced overhead of callback functions * Allows for easier testing and reuse of code Cons: * May be less readable and understandable for those familiar with traditional switch statements * Requires additional setup and understanding of functional programming concepts **Library and Purpose** The `isFunc` function is a utility function that checks if a given value is a function. This is used in the `funcccy` function to ensure that the callback functions passed to it are indeed functions. **Special JS Features or Syntax** There are no special JavaScript features or syntax mentioned in this benchmark that would require additional explanation. However, it's worth noting that the use of functional programming concepts like callback functions and higher-order functions is a relatively advanced topic. **Other Alternatives** Some other approaches to handling switch statements in JavaScript include: * Using `const cases = { ... };` and `switch (input) { ... }` syntax, which can provide better performance than traditional switch statements * Using libraries like [SwitchJS](https://github.com/luwes/switchjs) or [Switch Statement](https://github.com/tpmiller/switch-statement) that provide optimized implementation of switch statements It's worth noting that the choice of approach depends on the specific use case and performance requirements. **Benchmark Preparation Code Explanation** The script preparation code defines several functions: * `use`: a utility function that takes two arguments (a value and a function) and applies the function to the value. * `isFunc`: a utility function that checks if a given value is a function. * `isDefined`: a utility function that checks if a given value is not undefined or null. * `switchEnum`: a function that uses the `use` and `isFunc` functions to apply a callback function to the result of the switch statement. The benchmark preparation code then defines the `normal` and `funcccy` functions, which use these utility functions to implement the vanilla switch and functional approaches, respectively.
Related benchmarks:
Switch vs Object Literal w/out console.log 4
map vs ifelse vs switch test
Switch vs Functional Approach
Switch/case vs indexOf
Comments
Confirm delete:
Do you really want to delete benchmark?