Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Perfomance: Switch (with functions) vs Polymorphism (with methods)
(version: 1)
https://stackoverflow.com/questions/50401725/perfomance-switch-vs-polymorphism
Comparing performance of:
Polymorphism vs Switch
Created:
one year 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 }; function GetImportantValue1() { return 1; } function GetImportantValue2() { return 2; } function GetImportantValue3() { return 3; } function GetImportantValue4() { return 4; } function GetImportantValue5() { return 5; } getImportantValueSwitch = (myClassEnum) => { switch (myClassEnum.type) { case 'MyClass1': return GetImportantValue1(); case 'MyClass2': return GetImportantValue2(); case 'MyClass3': return GetImportantValue3(); case 'MyClass4': return GetImportantValue4(); case 'MyClass5': return GetImportantValue5(); } } 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:
4 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36
Browser/OS:
Chrome 142 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Polymorphism
277301984.0 Ops/sec
Switch
278127072.0 Ops/sec
Related benchmarks:
Perfomance: Switch vs Polymorphism
Property assignment vs Variable assignment
class vs function composition
Class vs object: direct call vs bind vs apply
Classnames vs CLSX vs custom implementation 01
object map vs switch 「!!!!」
Class field access speed v1
Wrapped class field access speed v1
"this" property vs. closure upvalue
Comments
Confirm delete:
Do you really want to delete benchmark?