Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Perfomance: Switch vs Polymorphism
(version: 0)
https://stackoverflow.com/questions/50401725/perfomance-switch-vs-polymorphism
Comparing performance of:
Polymorphism vs Switch
Created:
7 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var class1 = { GetImportantValue: () => 1 }; var class2 = { GetImportantValue: () => 2 }; var class3 = { GetImportantValue: () => 3 }; var class4 = { GetImportantValue: () => 4 }; var class5 = { GetImportantValue: () => 5 }; getImportantValueSwitch = (myClassEnum) => { switch (myClassEnum.type) { case 'MyClass1': return 1; case 'MyClass2': return 2; case 'MyClass3': return 3; case 'MyClass4': return 4; case 'MyClass5': return 5; } } getImportantValuePolymorphism = (myClass) => myClass.GetImportantValue();
Tests:
Polymorphism
getImportantValuePolymorphism(class1); getImportantValuePolymorphism(class2); getImportantValuePolymorphism(class3); getImportantValuePolymorphism(class4); getImportantValuePolymorphism(class5);
Switch
getImportantValueSwitch({type: 'MyClass1'}); getImportantValueSwitch({type: 'MyClass2'}); getImportantValueSwitch({type: 'MyClass3'}); getImportantValueSwitch({type: 'MyClass4'}); getImportantValueSwitch({type: 'MyClass5'});
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Polymorphism
Switch
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
23 days ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/147.0.0.0 Safari/537.36
Browser/OS:
Chrome 147 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Polymorphism
199220752.0 Ops/sec
Switch
212090240.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the explanation of the benchmark. **Benchmark Overview** The provided JSON represents a JavaScript performance benchmark that compares two approaches: switch statements and polymorphism (using method overriding). The goal is to determine which approach is faster for calling methods on different classes. **Switch Statement Approach** In this approach, the `getImportantValue` function uses a switch statement to determine which value to return based on the type of the input object. This requires creating multiple cases for each possible input type (in this case, five types). **Pros and Cons:** Pros: * Easy to implement and maintain * Can be more readable in certain situations Cons: * May lead to longer execution times due to the overhead of evaluating multiple cases * Requires explicit case management **Polymorphism Approach** In this approach, the `getImportantValue` function calls a method named `GetImportantValue` on the input object. This method is expected to be overridden in child classes. **Pros and Cons:** Pros: * More concise and expressive * Can lead to more flexible and reusable code Cons: * May introduce additional overhead due to method lookup and invocation * Requires careful consideration of inheritance hierarchies and method overriding **Library Used** In this benchmark, the `class1` to `class5` variables represent objects that are used as test subjects for both approaches. These classes do not extend any other class, which means they don't inherit from a base class with an overridden `GetImportantValue` method. **Special JS Feature/ Syntax** None of the code snippets in this benchmark utilize any special JavaScript features or syntax, such as ES6 classes, async/await, or promises. However, it's worth noting that modern browsers and Node.js environments may still use some internal optimizations that might affect performance measurements. **Other Alternatives** Some alternative approaches to polymorphism could include: * Using interfaces instead of inheritance hierarchies * Employing a more explicit method dispatch mechanism (e.g., using a dictionary or map) * Avoiding method overriding altogether and relying on static methods instead It's also worth noting that the choice of approach often depends on the specific use case, performance requirements, and design constraints. This benchmark provides a controlled environment to compare two common alternatives in JavaScript programming.
Related benchmarks:
Classnames vs CLSX vs composeCssClass 03
Switch vs Functional Approach
bmm tests2
Switch/case vs indexOf
Comments
Confirm delete:
Do you really want to delete benchmark?