Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Jump vs switch case
(version: 0)
Comparing performance of:
switch case vs jump table
Created:
7 years ago
by:
Guest
Jump to the latest result
Tests:
switch case
function thing(j) { switch(j) { case 0: return "rah"; case 1: return "lah"; case 2: return "sah"; case 3: return "blah"; } } for(let i = 0; i < 100000; i++) { for(let j = 0; thing(j) != "blah"; j++) { } }
jump table
function rah() { return "rah"; } function lah() { return "lah"; } function sah() { return "sah"; } function blah() { return "blah"; } let jumpTable = [rah, lah, sah, blah]; for(let i = 0; i < 100000; i++) { for(let j = 0; jumpTable[j]() != "blah"; j++) { } }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
switch case
jump table
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
10 months ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64; rv:140.0) Gecko/20100101 Firefox/140.0
Browser/OS:
Firefox 140 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
switch case
840.1 Ops/sec
jump table
777.6 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Benchmark Explanation** The provided benchmark compares the performance of two approaches: `switch case` and `jump table`. Both approaches are used to achieve the same functionality, but with different implementations. **Switch Case Approach** In the `switch case` approach, a function named `thing` is defined that takes an integer as input. The function uses a `switch` statement to determine which string to return based on the input value. The function is then called in a loop 100,000 times, and another inner loop is used to ensure that the `return` value of `thing` is not equal to `"blah"`. This approach is likely to be slower due to the overhead of the `switch` statement and the need to iterate over all possible cases. **Jump Table Approach** In the `jump table` approach, four separate functions are defined: `rah`, `lah`, `sah`, and `blah`. Each function returns a string. A jump table is created that maps each input value to one of these functions using an array index. The function associated with the input value is then called in a loop 100,000 times, and another inner loop is used to ensure that the result is not equal to `"blah"`. This approach is likely to be faster due to the use of a jump table, which allows for direct branching and avoids the overhead of a `switch` statement. **Pros and Cons** * **Switch Case Approach:** + Pros: Easy to understand and maintain, does not require explicit memory management. + Cons: Slower due to overhead of `switch` statement, more code is required to achieve the same functionality. * **Jump Table Approach:** + Pros: Faster, less code required compared to switch case approach. + Cons: Requires explicit memory management, can be harder to understand and maintain. **Other Considerations** In this benchmark, the use of a `switch` statement and an array index as a jump table are two different approaches to achieving the same functionality. The choice between these approaches depends on the specific requirements of the project and personal preference. **Library Usage** There is no library usage in this benchmark. **Special JS Features or Syntax** There are no special JavaScript features or syntax used in this benchmark, such as async/await, promises, or generators. **Alternatives** Other alternatives to these approaches could include: * **Lookup Table**: A lookup table can be created that maps each input value to one of the functions. This approach is similar to the jump table approach but uses a data structure instead of an array index. * **Hash Table**: A hash table can be used to map each input value to one of the functions. This approach is faster than the lookup table approach but may require more memory. * **Dynamic Dispatch**: Dynamic dispatch can be used to call the correct function based on the input value. This approach is similar to the switch case and jump table approaches but uses a different technique to determine which function to call. These alternatives would require additional code and potentially more complex logic, but could offer performance benefits or other advantages depending on the specific requirements of the project.
Related benchmarks:
Switch vs Object Literal - SR Test
Switch vs Object Literal
Switch vs Object Literal extended
Switch case vs Object Literal
Switch/case vs indexOf
Comments
Confirm delete:
Do you really want to delete benchmark?