function thing(j)
{
switch(j)
{
case '0':
return "zero";
case '1':
return "one";
case '2':
return "two";
case '3':
return "three";
case '4':
return "four";
case '5':
return "five";
case '6':
return "six";
case '7':
return "seven";
case '8':
return "eight";
case '9':
return "nine";
}
}
for(let i = 0; i < 100000; i++)
{
thing((i % 10).toString());
}
function zero()
{
return "zero";
}
function one()
{
return "one";
}
function two()
{
return "two";
}
function three()
{
return "three";
}
function four()
{
return "four";
}
function five()
{
return "five";
}
function six()
{
return "six";
}
function seven()
{
return "seven";
}
function eight()
{
return "eight";
}
function nine()
{
return "nine";
}
let jumpMap = {
'0': zero,
'1': one,
'2': two,
'3': three,
'4': four,
'5': five,
'6': six,
'7': seven,
'8': eight,
'9': nine
};
for(let i = 0; i < 100000; i++)
{
jumpMap[(i % 10).toString()]();
}
function zero()
{
return "zero";
}
function one()
{
return "one";
}
function two()
{
return "two";
}
function three()
{
return "three";
}
function four()
{
return "four";
}
function five()
{
return "five";
}
function six()
{
return "six";
}
function seven()
{
return "seven";
}
function eight()
{
return "eight";
}
function nine()
{
return "nine";
}
let jumpMap = new Map();
jumpMap.set('0', zero)
jumpMap.set('1', one)
jumpMap.set('2', two)
jumpMap.set('3', three)
jumpMap.set('4', four)
jumpMap.set('5', five)
jumpMap.set('6', six)
jumpMap.set('7', seven)
jumpMap.set('8', eight)
jumpMap.set('9', nine)
for(let i = 0; i < 100000; i++)
{
jumpMap.get((i % 10).toString())();
}
function thing(j)
{
if (j === '0') {
return 'zero';
} else if (j === '1') {
return 'one';
} else if (j === '2') {
return 'two';
} else if (j === '3') {
return 'three';
} else if (j === '4') {
return 'four';
} else if (j === '5') {
return 'five';
} else if (j === '6') {
return 'six';
} else if (j === '7') {
return 'seven';
} else if (j === '8') {
return 'eight';
} else if (j === '9') {
return 'nine';
}
}
for(let i = 0; i < 100000; i++)
{
thing((i % 10).toString());
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
switch case | |
jump hash map object | |
jump map instance | |
else if |
Test name | Executions per second |
---|---|
switch case | 924.6 Ops/sec |
jump hash map object | 1108.2 Ops/sec |
jump map instance | 790.6 Ops/sec |
else if | 803.1 Ops/sec |
Let's break down what's being tested in the provided benchmark.
Benchmark Goal:
The goal of this benchmark is to compare the performance of three different approaches for handling non-integer values:
switch
statement to handle different cases.else if
statements to handle different cases.Options Comparison:
The benchmark compares the performance of these three approaches for a large number of iterations (100,000). The goal is to determine which approach is the fastest.
Benchmark Results:
The latest benchmark results show that:
Observations:
The results suggest that using a hash map (Jump Hash Map Object) is the most efficient approach, likely due to its ability to directly access and execute the corresponding function for a given input value without the overhead of branching statements (switch case or else if). The switch case approach might incur additional overhead due to the number of branches and potential mispredictions by the JavaScript engine. The else if approach seems to be the slowest, possibly due to the need for multiple branches and potential stack overflows.
Keep in mind that these results are specific to Firefox 121 running on Linux Desktop, and the performance may vary across different browsers, platforms, and use cases.