Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Jump vs switch case vs else if for non-integers
(version: 1)
Using a jump hash map vs switch / case vs else if for non-integer values
Comparing performance of:
switch case vs jump hash map object vs jump map instance vs else if
Created:
3 years ago
by:
Registered User
Jump to the latest result
Tests:
switch case
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()); }
jump hash map object
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()](); }
jump map instance
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())(); }
else if
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()); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
switch case
jump hash map object
jump map instance
else if
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64; rv:121.0) Gecko/20100101 Firefox/121.0
Browser/OS:
Firefox 121 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
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
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
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: 1. **Switch Case**: Using a `switch` statement to handle different cases. 2. **Jump Hash Map Object**: Using a hash map (JavaScript object) with functions as values, where the key is a string representation of the input value and the function returns a corresponding result. 3. **Else If**: Using multiple `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: * **Jump Hash Map Object**: Performs best with an average execution rate of 924.63 executions per second. * **Switch Case**: Comes in second with an average execution rate of 803.05 executions per second. * **Else If**: Performs the worst with an average execution rate of 790.64 executions per second. **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.
Related benchmarks:
contains misses: Map vs Set vs Array vs Symbol vs UID
floor vs trunc vs bit shift
Object vs Map lookup w/ rando integer key
Hex convertion : Mapping vs Range
Switch/case vs indexOf
Comments
Confirm delete:
Do you really want to delete benchmark?