Run details:
Mozilla/5.0 (Linux; Android 7.0; SM-G935V Build/NRD90M; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/66.0.3359.158 Mobile Safari/537.36
Safari537.36
Android 7.0
Mobile
6 years ago
Test name Executions per second
Polymorphism 192882.8 Ops/sec
Switch 258479.4 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'});