Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object Literal vs If Else vs Switch Case
(version: 0)
Benchmark of Object Literal vs If Else vs Switch Case
Comparing performance of:
Object Literal vs If Else vs Switch Case vs Object Literal 2
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
animal = "cheetah"
Tests:
Object Literal
const babyAnimals = { dog: "Puppy", cat: "Kitten", cheetah: "Cub", }; let result1 = babyAnimals[animal] || "-";
If Else
let result2 = "-"; if (animal === "dog") result2 = "Puppy"; else if (animal === "cat") result2 = "Kitten"; else if (animal === "cheetah") result2 = "Cub";
Switch Case
let result3 = "-"; switch (animal) { case "dog": result3 = "Puppy"; break; case "cat": result3 = "Kitten"; break; case "cheetah": result3 = "Cub"; break; default: }
Object Literal 2
const babyAnimals = { dog: "Puppy", cat: "Kitten", cheetah: "Cub", }; let result1 = babyAnimals[animal] ?? "-";
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Object Literal
If Else
Switch Case
Object Literal 2
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36
Browser/OS:
Chrome 135 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Object Literal
86791152.0 Ops/sec
If Else
94567760.0 Ops/sec
Switch Case
98209192.0 Ops/sec
Object Literal 2
92953224.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript benchmarks. **Benchmark Overview** The provided benchmark compares three different approaches to retrieve values from an object: Object Literal, If-Else statement, and Switch Case statement. The benchmark uses a simple scenario where it checks if a specific animal (e.g., "cheetah") exists in an object containing various animals (dog, cat, cheetah) as keys. **Options Compared** 1. **Object Literal**: Using the object literal syntax to access the value of the specified animal. 2. **If-Else statement**: Using an if-else statement with conditional checks to retrieve the value of the specified animal. 3. **Switch Case statement**: Using a switch case statement to retrieve the value of the specified animal. **Pros and Cons** 1. **Object Literal** * Pros: concise, easy to read, and maintain. * Cons: may lead to object size issues if not properly indexed. 2. **If-Else statement** * Pros: flexible, can handle complex logic. * Cons: verbose, harder to read and maintain. 3. **Switch Case statement** * Pros: concise, easy to read, and maintain. * Cons: requires the switch value to be an exact match (no partial matches). **Other Considerations** * The `??` operator (introduced in ECMAScript 2017) is used as a safer alternative to object literal access. It returns the first operand if it's truthy, and the second operand if it's falsy. * The `||` operator is used to provide a default value when the specified animal is not found in the object. **Library Usage** The benchmark does not explicitly use any libraries. However, it relies on the JavaScript engine's built-in features, such as object literals, if-else statements, and switch case statements. **Special JS Features/Syntax** None mentioned in this specific benchmark. **Benchmark Results** The latest benchmark results show that: 1. **Object Literal 2** (using `??` operator) has the highest executions per second (4718195.0), indicating it's the most efficient approach. 2. **Switch Case** follows closely, with a lower executions per second value (4592487.0). 3. **If-Else statement** has the lowest executions per second value (1571642.375). Keep in mind that these results might vary depending on the specific JavaScript engine and platform used. **Alternatives** For similar benchmarks, you can explore other scenarios, such as: * Comparing different data structures (e.g., arrays, sets, maps) * Measuring performance of algorithms (e.g., sorting, searching) * Evaluating the impact of various optimization techniques (e.g., caching, memoization)
Related benchmarks:
Switch vs Object Function vs Object Property
another approach to Switch vs Object Literal
Switch vs Object Literal in javascript
Switch vs Object Literal in javascript 1
Switch vs Object Literal - no console.log
Comments
Confirm delete:
Do you really want to delete benchmark?