var map2 = new Map();
map2.set('cat', 'Kitten');
map2.set('cattle', 'Calf');
map2.set('cheetah', 'Cub');
map2.set('dog', 'Pup');
var babyAnimal2 = {
cat: 'Kitten',
cattle: 'Calf',
cheetah: 'Cub',
dog: 'Pup'
}
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))
var animal = ["dog", "cat", "cattle", "cheetah"][Math.floor(Math.random() * 4)]
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))
var animal = ["dog", "cat", "cattle", "cheetah"][Math.floor(Math.random() * 4)]
function test(animal) {
if(animal==='cat'){
return 'Kitten'
} else if(animal=='cattle'){
return 'Calf'
} else if(animal==='cheetah'){
return 'Cub';
} else if(animal==='dog'){
return 'Pup';
}
return "I don't know that"
}
console.log(test(animal))
var animal = ["dog", "cat", "cattle", "cheetah"][Math.floor(Math.random() * 4)]
function test(animal) {
return babyAnimal2[animal] ?? "I don't know that"
}
console.log(test(animal))
var animal = ["dog", "cat", "cattle", "cheetah"][Math.floor(Math.random() * 4)]
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))
var animal = ["dog", "cat", "cattle", "cheetah"][Math.floor(Math.random() * 4)]
function test(animal) {
return map2.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 | 61848.5 Ops/sec |
Object Literal | 57390.3 Ops/sec |
If Else | 55407.1 Ops/sec |
Object Literal defined outside function | 59543.3 Ops/sec |
Map | 55664.2 Ops/sec |
Map - defined outsid function | 62178.4 Ops/sec |
Let's break down the benchmark and explain what's being tested, the pros and cons of each approach, and other considerations.
What is being tested?
The benchmark compares four different approaches to access an array element:
switch
statement to check the value of the animal
variable.animal
variable.Map
data structure to store and retrieve the elements.Options compared
The benchmark compares these four approaches on the same input data:
[["dog", "cat", "cattle", "cheetah"][Math.floor(Math.random() * 4)]]
Pros and Cons of each approach
Other considerations
RawUAString
and other metadata provide information about the browser's behavior, but it's not directly related to the code performance.In summary, this benchmark compares four approaches to access an array element: switch statement, object literal (nested), if-else chain, and map data structure. While each approach has its pros and cons, the results suggest that the Map
data structure performs best in terms of execution speed.