var animal = ["dog", "cat", "cattle", "cheetah"][Math.floor(Math.random() * 4)]
function test(animal) {
switch(animal){
case 'cat': return 'Kitten'
case 'cattle': return 'Calf'
case 'cheetah': return 'Cub'
case 'dog': return 'Pup'
default: return "I don't know that"
}
}
console.log(test(animal))
function test(animal) {
var babyAnimal = {
cat:'Kitten',
cattle:'Calf',
cheetah:'Cub',
dog:'Pup'
}
return babyAnimal[animal] ?? "I don't know that"
}
console.log(test(animal))
function test(animal) {
if(animal.toLowerCase()==='cat'){
return 'Kitten'
} else if(animal.toLowerCase()=='cattle'){
return 'Calf'
} else if(animal.toLowerCase()==='cheetah'){
return 'Cub';
} else if(animal.toLowerCase()==='dog'){
return 'Pup';
}
return "I don't know that"
}
console.log(test(animal))
var babyAnimal = {
cat:'Kitten',
cattle:'Calf',
cheetah:'Cub',
dog:'Pup'
}
function test(animal) {
return babyAnimal[animal] ?? "I don't know that"
}
console.log(test(animal))
function test(animal) {
const map1 = new Map();
map1.set('cat', 'Kitten');
map1.set('cattle', 'Calf');
map1.set('cheetah', 'Cub');
map1.set('dog', 'Pup');
return map1.get(animal) || "I don't know that"
}
console.log(test(animal))
const map1 = new Map();
map1.set('cat', 'Kitten');
map1.set('cattle', 'Calf');
map1.set('cheetah', 'Cub');
map1.set('dog', 'Pup');
function test(animal) {
return map1.get(animal) || "I don't know that"
}
console.log(test(animal))
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Switch | |
Object Literal | |
If Else | |
Object Literal defined outside function | |
Map | |
Map - defined outsid function |
Test name | Executions per second |
---|---|
Switch | 114512.4 Ops/sec |
Object Literal | 105790.7 Ops/sec |
If Else | 101441.0 Ops/sec |
Object Literal defined outside function | 78695.8 Ops/sec |
Map | 87752.2 Ops/sec |
Map - defined outsid function | 93376.6 Ops/sec |
I'll break down the provided benchmark and explain each test case.
Benchmark Overview
The benchmark compares four different approaches to look up values in a small dataset: switch
, object literal
(also known as bracket notation), if-else
statement, and Map
. The goal is to determine which approach is fastest.
Test Cases
function test(animal) {
switch(animal){
case 'cat': return 'Kitten'
case 'cattle': return 'Calf'
case 'cheetah': return 'Cub'
case 'dog': return 'Pup'
default: return "I don't know that"
}
}
The switch
statement is used to execute a block of code based on the value of the animal
variable. The break
keyword is implied after each case
clause.
Pros:
Cons:
function test(animal) {
var babyAnimal = {
cat:'Kitten',
cattle:'Calf',
cheetah:'Cub',
dog:'Pup'
};
return babyAnimal[animal] ?? "I don't know that";
}
This approach uses an object literal to store the lookup values. The ??
operator is used to provide a default value if the key (animal
) is not present in the object.
Pros:
Cons:
function test(animal) {
if(animal.toLowerCase()==='cat'){
return 'Kitten'
} else if(animal.toLowerCase()=='cattle'){
return 'Calf'
} else if(animal.toLowerCase()==='cheetah'){
return 'Cub';
} else if(animal.toLowerCase()=='dog'){
return 'Pup';
} else {
return "I don't know that";
}
}
This approach uses a series of if-else
statements to check the value of the animal
variable and perform the corresponding action.
Pros:
Cons:
function test(animal) {
var map = new Map();
map.set('cat', 'Kitten');
map.set('cattle', 'Calf');
map.set('cheetah', 'Cub');
map.set('dog', 'Pup');
return map.get(animal) ?? "I don't know that";
}
This approach uses a Map
object to store the lookup values. The get()
method is used to retrieve the value associated with the key (animal
).
Pros:
Cons:
Results
The benchmark results show that Object Literal
is the fastest approach, followed closely by Switch
. The If-Else
statement is slower due to the need for multiple checks. The Map
approach is also relatively fast but slower than the first two.
Keep in mind that these results are specific to this particular dataset and may vary depending on the size and complexity of the data.