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;
};
};
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(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;
functionSwitchLoop();
currentState = IDLE;
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
if loop | |
while + switch/case loop | |
function + switch/case loop |
Test name | Executions per second |
---|---|
if loop | 9181.6 Ops/sec |
while + switch/case loop | 9104.1 Ops/sec |
function + switch/case loop | 8816.0 Ops/sec |
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:
Pros and Cons of each approach:
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).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:
These alternatives would depend on the specific requirements and complexity of the state machine being implemented.