Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Functional Match vs Switch
(version: 0)
Comparing performance of:
Functional Match vs Switch
Created:
6 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var str = 'abc'; str = str.charAt(Math.floor(Math.random() * 3));
Tests:
Functional Match
const match = x => { const matched = x => ({ on: () => matched(x), otherwise: () => x, }); return { on: (pred, fn) => ( pred(x) ? matched(fn(x)) : match(x) ), otherwise: fn => fn(x), }; } match(str) .on(x => x === 'a', () => console.log('A')) .on(x => x === 'b', () => console.log('B')) .on(x => x === 'c', () => console.log('C')) .otherwise(() => console.log('default'));
Switch
switch (str) { case 'a': console.log('A'); break; case 'b': console.log('B'); break; case 'c': console.log('C'); break; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Functional Match
Switch
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'd be happy to explain the benchmark and its results. **Benchmark Description** The provided benchmark is designed to compare the performance of two approaches: functional matching using JavaScript's `match` function (formerly known as `String.prototype.match`) and traditional switch statements in JavaScript. The test creates a random string variable `str` with three possible characters ('a', 'b', or 'c') and then uses the `match` function to create an object with two methods: `on` and `otherwise`. The `on` method takes a predicate function that checks if the input `x` matches one of the expected values, and returns either another `match` function or the original value. If no match is found, it calls the `otherwise` method with the original value as an argument. **Options Compared** The two options being compared are: 1. **Functional Match**: Using JavaScript's `match` function to create a flexible and expressive matching mechanism. 2. **Switch Statement**: A traditional, imperative approach using a switch statement to handle different cases. **Pros and Cons of Each Approach** **Functional Match:** Pros: * Flexible and expressive, allowing for complex logic and multiple branching paths * Can be easily composed with other functions or methods * Does not require explicit case statements Cons: * May have performance overhead due to function calls and object creation * Can lead to slower execution times if the predicate is expensive to evaluate **Switch Statement:** Pros: * Fast execution times, as it uses a simple jump table lookup * Easy to understand and maintain, especially for small number of cases Cons: * Less flexible than functional matching, as it requires explicit case statements * Can lead to code duplication if multiple cases share similar logic **Library and Special JS Features** In this benchmark, the `match` function is used, which is a part of JavaScript's String prototype. The `match` function takes a regular expression pattern and returns an array containing match data or null if no match. There are no special JavaScript features being tested in this benchmark. **Other Alternatives** If you're interested in exploring other alternatives to functional matching and switch statements, here are a few options: 1. **Regular Expressions**: You can use regular expressions to create complex matching logic, but they can be slower than the `match` function. 2. **Object-Oriented Programming (OOP) Techniques**: OOP techniques like inheritance and polymorphism can provide more flexibility and expressiveness than switch statements or functional matching. 3. **Declarative Programming Languages**: Declarative programming languages like Prolog or Mercury can provide a more concise and expressive way of defining matching logic. Overall, the choice between functional matching and switch statements depends on the specific requirements of your project, including performance, maintainability, and expressiveness.
Related benchmarks:
Switch vs Object Literal w/out console.log 2
Switch vs Object Literal methods
Switch vs Object Literal 24r34rf3rr
Switch vs Object Literal value return
char index vs charAt() vs slice() random access
Comments
Confirm delete:
Do you really want to delete benchmark?