Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
if loop vs while + switch/case loop vs function + switch/case loop
(version: 2)
Comparing performance of:
if loop vs while + switch/case loop vs function + switch/case loop
Created:
4 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var IDLE = 0; var CONSOLE_LOG = 1; var CONSOLE_WARN = 2; var CONSOLE_ERROR = 3; var CONSOLE_INFO = 4; var COMPLETE = 5; var currentState = IDLE; var string = 'Hello World!'; function functionSwitchLoop() { switch(currentState) { case IDLE: currentState = CONSOLE_LOG; return functionSwitchLoop(); case CONSOLE_LOG: console.log(string); currentState = CONSOLE_WARN; return functionSwitchLoop(); case CONSOLE_WARN: console.warn(string); currentState = CONSOLE_ERROR; return functionSwitchLoop(); case CONSOLE_ERROR: console.error(string); currentState = CONSOLE_INFO; return functionSwitchLoop(); case CONSOLE_INFO: console.info(string); return currentState = COMPLETE; }; };
Tests:
if loop
if(currentState === IDLE) { currentState = CONSOLE_LOG; }; if(currentState === CONSOLE_LOG) { console.log(string); currentState = CONSOLE_WARN; }; if(currentState === CONSOLE_WARN) { console.warn(string); currentState = CONSOLE_ERROR; }; if(currentState === CONSOLE_ERROR) { console.error(string); currentState = CONSOLE_INFO; }; if(currentState === CONSOLE_INFO) { console.info(string); currentState = COMPLETE; }; if(currentState === COMPLETE) currentState = IDLE;
while + switch/case loop
while(currentState != COMPLETE) { switch(currentState) { case IDLE: currentState = CONSOLE_LOG; break; case CONSOLE_LOG: console.log(string); currentState = CONSOLE_WARN; break; case CONSOLE_WARN: console.warn(string); currentState = CONSOLE_ERROR; break; case CONSOLE_ERROR: console.error(string); currentState = CONSOLE_INFO; break; default: console.info(string); currentState = COMPLETE; }; }; currentState = IDLE;
function + switch/case loop
functionSwitchLoop(); currentState = IDLE;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
if loop
while + switch/case loop
function + switch/case loop
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark definition and test cases. **Benchmark Definition:** The benchmark is comparing four different approaches to manage a state machine with multiple states, logging levels, and transitions between them: 1. **If Loop**: A traditional if-else statement chain to determine the next state. 2. **While + Switch/Case Loop**: A while loop that iterates until the complete state is reached, using a switch statement to handle state changes. 3. **Function + Switch/Case Loop**: A function that calls itself recursively to iterate through states, with a switch statement to manage state transitions. **Pros and Cons of each approach:** 1. **If Loop**: * Pros: Simple, straightforward, and easy to understand. * Cons: Can be slow due to the overhead of multiple conditional checks. 2. **While + Switch/Case Loop**: * Pros: Can be faster than if loops due to fewer checks, but may lead to more complex logic. * Cons: Requires careful consideration of loop control and potential performance issues with large numbers of states. 3. **Function + Switch/Case Loop**: * Pros: Encapsulates the state machine logic within a reusable function, making it easier to maintain. * Cons: May introduce additional overhead due to function calls and recursive execution. **Library usage:** The benchmark code uses the `console` object from the browser's API to log messages at different levels (info, warn, error). This is not a library in the classical sense, but rather an API that provides logging functionality. However, if we consider external libraries used by the test cases (none are explicitly mentioned), they might be: * `lodash` or similar utility libraries for string manipulation (e.g., splitting `string` into individual characters). * No explicit mentions of other libraries in the provided code. **Special JS features:** None of the benchmark definitions use any special JavaScript features, such as async/await, generators, or ES6 classes. The code is written in a straightforward, conventional style. **Alternatives:** Other approaches to managing state machines might include: * **State Machines**: Pre-built libraries that provide a structured way to manage states, transitions, and events. * **Finite State Machines**: Mathematical frameworks for modeling and analyzing complex systems with finite states. * **Behavior Trees**: Data-driven graphs used to represent behavior in games and simulations. These alternatives would depend on the specific requirements and complexity of the state machine being implemented.
Related benchmarks:
Switch vs Object Literal w/out console.log 2
Switch vs Object Literal w/out console.log 4
Nath's Switch vs Object Literal Test
Switch vs Object Literal Performance Check case 40
Comments
Confirm delete:
Do you really want to delete benchmark?