var str = 'abcdef';
str = str.charAt(Math.floor(Math.random() * 3));
switch (str) {
case 'a': console.log('A'); break;
case 'b': console.log('B'); break;
case 'c': console.log('C'); break;
case 'd': console.log('D'); break;
case 'e': console.log('E'); break;
case 'f': console.log('F'); break;
}
var objLiteral = {
a: function() {
console.log('A');
},
b: function() {
console.log('B');
},
c: function() {
console.log('C');
},
d: function() {
console.log('D');
},
e: function() {
console.log('E');
},
f: function() {
console.log('F');
}
}
objLiteral[str]();
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Switch | |
Object Literal |
Test name | Executions per second |
---|---|
Switch | 534599.8 Ops/sec |
Object Literal | 495240.4 Ops/sec |
Benchmark Overview
The provided benchmark compares the performance of two approaches: switch
statements and object literals in JavaScript.
What is being tested?
In this benchmark, the script preparation code generates a random string str
with 3 possible characters ('a', 'b', or 'c'). The script then uses either a switch
statement or an object literal to determine which function to call based on the last character of the string.
Options being compared
The two options being compared are:
switch
statement is used to check the value of str
against a set of predefined cases.str
and the corresponding functions to call.Pros and Cons
Library and Purpose
In this benchmark, there is no specific library being used. However, it's worth noting that some JavaScript engines may optimize certain features or syntax, such as switch
statements with long chains of cases.
Special JS feature or syntax
There are no special JS features or syntaxes being used in this benchmark other than the standard switch
statement and object literals.
Other alternatives
Other alternatives to these approaches could include:
lookupTable
array to store the functions to call based on the character valuesHowever, it's worth noting that these alternatives may not provide significant performance improvements over the switch
statement and object literal approaches.
Benchmark Preparation Code
The provided script preparation code is:
var str = 'abcdef';
str = str.charAt(Math.floor(Math.random() * 3));
This code generates a random string str
with 3 possible characters ('a', 'b', or 'c') and assigns it to the variable after generating a new random index.
Individual Test Cases
The two individual test cases are:
switch
statement to determine which function to call based on the last character of the string.switch (str) {
case 'a':
console.log('A');
break;
case 'b':
console.log('B');
break;
case 'c':
console.log('C');
break;
case 'd':
console.log('D');
break;
case 'e':
console.log('E');
break;
case 'f':
console.log('F');
break;
}
str
and the corresponding functions to call.var objLiteral = {
a: function() { console.log('A'); },
b: function() { console.log('B'); },
c: function() { console.log('C'); },
d: function() { console.log('D'); },
e: function() { console.log('E'); },
f: function() { console.log('F'); }
}
objLiteral[str]();