var testString = "abc.bcd.cde.def.asdb.asdb.asdb.asdb.asdb.asdb.asdb.asdb.asdb.asdb.asdb.asdb.asdb.asdb.asdb.asdb.asdb.asdb.asdb.asdb.asdb"
testString.split(".").forEach(str => {
if(["abc","bcd","cde","def"].includes(str)){
switch(str){
case "abc":
console.log(str);
case "bcd":
console.log(str);
case "cde":
console.log(str);
case "def":
console.log(str);
default:
}
}
});
testString.split(".").forEach(str => {
if(str === "abc"){
console.log(str);
} else if(str === "bcd"){
console.log(str);
} else if(str === "cde"){
console.log(str);
} else if(str === "def"){
console.log(str);
}
});
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
switch with includes | |
if-else |
Test name | Executions per second |
---|---|
switch with includes | 22443.1 Ops/sec |
if-else | 50784.7 Ops/sec |
Let's dive into the explanation.
The benchmark tests two different approaches to switch between multiple values in JavaScript: using an if-else
statement or using a switch
statement with an array of expected values and the includes()
method.
Test Case 1: Switch with includes
This test case uses a switch
statement with an array of expected values (["abc", "bcd", "cde", "def"]
) and checks if each value is included in that array. If it is, the corresponding console.log(str)
statement is executed.
The code snippet:
var testString = "abc.bcd.cde.def.asdb.asdb.asdb.asdb.asdb.asdb.asdb.asdb.asdb.asdb.asdb.asdb.asdb.asdb.asdb.asdb.asdb.asdb.asdb";
testString.split(".").forEach(str => {
if(["abc","bcd","cde","def"].includes(str)){
switch(str){
case "abc":
console.log(str);
case "bcd":
console.log(str);
case "cde":
console.log(str);
case "def":
console.log(str);
default:
}
}
});
Test Case 2: If-else
This test case uses an if-else
statement chain to switch between multiple values. For each value, a separate console.log(str)
statement is executed.
The code snippet:
var testString = "abc.bcd.cde.def.asdb.asdb.asdb.asdb.asdb.asdb.asdb.asdb.asdb.asdb.asdb.asdb.asdb.asdb.asdb.asdb.asdb.asdb";
testString.split(".").forEach(str => {
if(str === "abc"){
console.log(str);
} else if(str === "bcd"){
console.log(str);
} else if(str === "cde"){
console.log(str);
} else if(str === "def"){
console.log(str);
}
});
Comparison
The benchmark results show that the if-else
approach is significantly slower (8876.55 executions per second) compared to the switch with includes
approach (21,573.59 executions per second). This suggests that using a switch
statement with an array of expected values can be a more efficient way to switch between multiple values in JavaScript.
Pros and Cons
if-else
statement chainif
statementsOther Considerations
switch with includes
and if-else
depends on the specific use case and personal preference. If conciseness and performance are priorities, switch with includes
might be a better choice.if-else
statement chain or another approach should be used to ensure accurate switching behavior.No external libraries or special JS features are used in these test cases. The only JavaScript feature used is the includes()
method, which checks if a value exists within an array.