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();
getImportantValuePolymorphism(class1);
getImportantValuePolymorphism(class2);
getImportantValuePolymorphism(class3);
getImportantValuePolymorphism(class4);
getImportantValuePolymorphism(class5);
getImportantValueSwitch({type: 'MyClass1'});
getImportantValueSwitch({type: 'MyClass2'});
getImportantValueSwitch({type: 'MyClass3'});
getImportantValueSwitch({type: 'MyClass4'});
getImportantValueSwitch({type: 'MyClass5'});
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Polymorphism | |
Switch |
Test name | Executions per second |
---|---|
Polymorphism | 4459813.0 Ops/sec |
Switch | 2767863.5 Ops/sec |
In this benchmark, two approaches for determining values associated with classes are compared: polymorphism using methods and a switch statement. The purpose is to evaluate their performance in terms of execution speed.
Polymorphism (with methods)
GetImportantValue()
that returns its respective value. The polymorphic behavior allows the code to call the method directly without needing to know the type of class at the time of calling.getImportantValuePolymorphism
for each class (class1 to class5). The function simply invokes the method GetImportantValue()
of each class.Switch Statement
switch
statement to determine which function to call based on an enumeration type passed to it. Each class is represented by a type string, and a corresponding function is called in the switch
based on that string.getImportantValueSwitch
function, passing an object containing a type property (like 'MyClass1', 'MyClass2', etc.) which directs the program flow to the correct case in the switch block.Polymorphism Pros:
Polymorphism Cons:
Switch Pros:
Switch Cons:
In this benchmark, no special JavaScript libraries or features are utilized beyond basic functions and structures. The benchmark mainly uses core JavaScript syntax and capabilities.
In conclusion, this JavaScript benchmark serves to evaluate two common methods for value retrieval through classes, showcasing the trade-offs between simplicity and optimization in code design. Each method has its unique strengths and weaknesses that can significantly affect performance depending on use cases.