Run details:
Mozilla/5.0 (iPhone; CPU iPhone OS 16_5_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.5.2 Mobile/15E148 Safari/604.1
Mobile Safari 16
iOS 16.5.1
Mobile
one year ago
Test name Executions per second
Polymorphism 1916967.0 Ops/sec
Switch 3210395.0 Ops/sec
Script Preparation code:
x
 
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'});