Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Switch vs Object Literal vs If Else vs Map - testing with simpler data again
(version: 0)
Comparing performance of:
Switch vs Object Literal vs If Else vs Object Literal defined outside function vs Map vs Map - defined outsid function
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
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' }
Tests:
Switch
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))
Object Literal
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))
If Else
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))
Object Literal defined outside function
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))
Map
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))
Map - defined outsid function
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))
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (6)
Previous results
Fork
Test case name
Result
Switch
Object Literal
If Else
Object Literal defined outside function
Map
Map - defined outsid function
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
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: 1. **Switch statement**: The code uses a `switch` statement to check the value of the `animal` variable. 2. **Object Literal (nested)**: The code uses an object literal with nested properties to access the element. 3. **If-Else chain**: The code uses an if-else chain to check the value of the `animal` variable. 4. **Map data structure**: The code uses a `Map` data structure to store and retrieve the elements. **Options compared** The benchmark compares these four approaches on the same input data: * An array with random values: `[["dog", "cat", "cattle", "cheetah"][Math.floor(Math.random() * 4)]]` * The corresponding value for each element in the array (e.g., "cat" for the first element, "Kitten") **Pros and Cons of each approach** 1. **Switch statement**: * Pros: Fast lookup times and concise code. * Cons: Can be brittle if the number of cases increases or changes. 2. **Object Literal (nested)**: * Pros: Flexible and easy to add new properties, but can become complex for large numbers of values. * Cons: May lead to slower performance due to nested lookups. 3. **If-Else chain**: * Pros: Easy to read and maintain, flexible for different logic paths. * Cons: Can be slower than switch statements or object literals, as it involves multiple checks. 4. **Map data structure**: * Pros: Fast lookup times, flexible for large numbers of values, and easy to add new elements. * Cons: Requires more memory to store the map. **Other considerations** * The benchmark uses a fixed input data set, which might not be representative of real-world scenarios. * The benchmark is run on Chrome 105 with a Desktop platform, which may affect performance compared to other browsers or platforms. * The `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.
Related benchmarks:
Array from() vs Map.keys()
Array from() vs Map.keys() vs Map.values() vs spread
Switch vs Object Literal vs If Else vs Map - testing with simpler data
Switch vs Object Literal vs If Else vs Map - testing with simpler data again again
Comments
Confirm delete:
Do you really want to delete benchmark?