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');
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');
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Vanilla Switch | |
Switch Fn style |
Test name | Executions per second |
---|---|
Vanilla Switch | 63907816.0 Ops/sec |
Switch Fn style | 8259009.5 Ops/sec |
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:
switch
statement to select the corresponding handler based on the input value.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:
funcccy
that takes an input value.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:
Cons:
switch
statement.Functional Approach (Switch Fn style)
Pros:
Cons:
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.