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,
});
}
normal('today');
normal('yesterday');
normal('tomorrow');
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 | 10824798.0 Ops/sec |
Switch Fn style | 6031819.0 Ops/sec |
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:
Cons:
switch
keywordFunctional Approach (Switch Fn style):
Pros:
Cons:
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:
const cases = { ... };
and switch (input) { ... }
syntax, which can provide better performance than traditional switch statementsIt'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.