function returnName(name) {
if (name === '1Y') {
return 'oneYearRor';
}
if (name === '3Y') {
return 'threeYearRor';
}
if (name === '5Y') {
return 'fiveYearRor';
}
if (name === '10Y') {
return 'tenYearRor';
}
if (name === 'SI') {
return 'sinceInceptionRor';
}
if (name === 'YTD') {
return null;
}
};
function switchStatement(name) {
switch(name) {
case '1Y': return 'oneYearRor';
case '3Y': return 'threeYearRor';
case '5Y': return 'fiveYearRor';
case '10Y': return 'tenYearRor';
case 'SI': return 'sinceInceptionRor';
default: return null;
}
}
returnName('1Y')
returnName('3Y')
returnName('5Y')
returnName('10Y')
returnName('SI')
returnName('YTD')
switchStatement('1Y')
switchStatement('3Y')
switchStatement('5Y')
switchStatement('10Y')
switchStatement('SI')
switchStatement('YTD')
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
If else statement | |
Switch statement |
Test name | Executions per second |
---|---|
If else statement | 289272800.0 Ops/sec |
Switch statement | 280381824.0 Ops/sec |
What is tested?
The provided JSON represents two JavaScript microbenchmarks: "If else statement" and "Switch statement". The benchmarks compare the performance of two common control structures in JavaScript: if-else statements and switch statements.
Options compared
In this benchmark, the following options are compared:
The benchmark tests how efficient each control structure is at determining the return value based on a set of predefined conditions.
Pros and Cons
Here's a brief analysis of the pros and cons of each option:
Library/Function
The provided benchmark uses a custom function returnName
that takes a string argument and returns a specific value based on the input. This function is used to create both if-else and switch statement benchmarks.
Special JS feature/syntax (None)
There are no special JavaScript features or syntaxes being tested in this benchmark.
Benchmark Preparation Code
The provided JSON contains two functions: returnName
for creating the if-else benchmark, and switchStatement
for creating the switch statement benchmark. These functions take a string argument as input and return a specific value based on the input.
Other alternatives
In general, if you need to compare control structures, you can also consider using other alternatives like:
===
or ==
.However, these alternatives may not provide a direct comparison between if-else and switch statements like the provided benchmark does.
In conclusion, this benchmark provides a simple yet effective way to compare the performance of two common control structures in JavaScript: if-else statements and switch statements.