Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Run results for:
Perfomance: Switch vs Polymorphism
https://stackoverflow.com/questions/50401725/perfomance-switch-vs-polymorphism
Go to the benchmark
Embed
Embed Benchmark Result
Run details:
User agent:
Mozilla/5.0 (iPhone; CPU iPhone OS 17_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) CriOS/122.0.6261.89 Mobile/15E148 Safari/604.1
Browser:
Chrome Mobile iOS 122
Operating system:
iOS 17.3
Device Platform:
Mobile
Date tested:
2 years ago
Test name
Executions per second
Polymorphism
1461062.9 Ops/sec
Switch
2293471.0 Ops/sec
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'});