Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Jump vs switch case v2
(version: 0)
Updated to separate function creation from test. Also added in alternatives for arrow functions and Map
Comparing performance of:
switch case vs jump table vs jump table (arrow functions) vs jump table (Map)
Created:
7 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
// Switch function thing(j) { switch(j) { case 0: return "rah"; case 1: return "lah"; case 2: return "sah"; case 3: return "blah"; } } // Jumptable function rah() { return "rah"; } function lah() { return "lah"; } function sah() { return "sah"; } function blah() { return "blah"; } var jumpTable = [rah, lah, sah, blah]; // Arrow functions var jumpTableArrow = [() => "rah", () => "lah", () => "sah", () => "blah"] // Map var jumpTableMap = new Map(jumpTableArrow.map((fun, i) => [i, fun]))
Tests:
switch case
for(let i = 0; i < 100000; i++) { for(let j = 0; thing(j) != "blah"; j++) { } }
jump table
for(let i = 0; i < 100000; i++) { for(let j = 0; jumpTable[j]() != "blah"; j++) { } }
jump table (arrow functions)
for(let i = 0; i < 100000; i++) { for(let j = 0; jumpTableArrow[j]() != "blah"; j++) { } }
jump table (Map)
for(let i = 0; i < 100000; i++) { for(let j = 0; jumpTableMap.get(j)() != "blah"; j++) { } }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
switch case
jump table
jump table (arrow functions)
jump table (Map)
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64; rv:138.0) Gecko/20100101 Firefox/138.0
Browser/OS:
Firefox 138 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
switch case
399.4 Ops/sec
jump table
100.0 Ops/sec
jump table (arrow functions)
105.7 Ops/sec
jump table (Map)
88.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and its test cases. **Benchmark Overview** The benchmark is designed to compare the performance of three different approaches for implementing a jump table: 1. **Switch statement**: A traditional switch statement with multiple cases. 2. **Jumptable**: A jump table implemented using an array of functions. 3. **Arrow functions**: Arrow functions used as a jump table. 4. **Map**: Using a Map data structure to implement the jump table. **Options Compared** The benchmark compares the performance of these four options: * Switch statement (traditional approach) * Jumptable (array-based approach) * Arrow functions (functional programming approach) * Map (data structure-based approach) **Pros and Cons** Here's a brief overview of each option: 1. **Switch Statement**: Pros: * Easy to implement * Good for small jump tables Cons: * Slow for large jump tables due to the overhead of checking multiple cases * Not suitable for dynamic data or runtime changes 2. **Jumptable**: Pros: * Fast lookup times using arrays * Suitable for both static and dynamic data Cons: * Requires manual indexing and management 3. **Arrow Functions**: Pros: * Concise and expressive syntax * Good for small to medium-sized jump tables Cons: * Less readable than traditional functions 4. **Map**: Pros: * Fast lookup times using Hash Tables (similar to Jumptable) * Suitable for both static and dynamic data Cons: * Requires additional memory to store the Map **Library Usage** In this benchmark, no libraries are explicitly mentioned, but Map is a built-in JavaScript object. **Special JS Features/Syntax** The arrow function syntax (`() =>`) is used in two test cases (jump table and jump table with Map). This is a relatively recent feature introduced in ECMAScript 2015, allowing for concise function expressions. **Other Considerations** When choosing an approach, consider the following factors: * **Size of the jump table**: For small tables, switch statements might be sufficient. For larger tables, Jumptable or Map might be more suitable. * **Dynamic data**: If your use case involves dynamic data, Jumptable or Map might be a better choice. * **Performance-critical code**: When performance is critical, Jumptable or Map might provide a slight performance advantage over switch statements. **Alternatives** Some alternative approaches not mentioned in this benchmark include: * Using a `switch` statement with a lookup table (e.g., an object) * Implementing a custom data structure, such as a trie * Using an interpreter or a dynamic compiler Keep in mind that the choice of approach ultimately depends on your specific use case and performance requirements.
Related benchmarks:
switch case vs jump table vs bounce pattern vs ternary
Jump vs switch case vs object literal
Jump table vs switch case
Jump table vs switch case
Comments
Confirm delete:
Do you really want to delete benchmark?