Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Switch vs Prepared Object Literal vs Prepared Array
(version: 0)
Comparing performance of:
Switch vs Object Literal vs Array Literal
Created:
5 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var objLiteral = { 0: function() { console.log('A'); }, 1: function() { console.log('B'); }, 2: function() { console.log('C'); } } var arrayLiteral = [ function() { console.log('A'); }, function() { console.log('B'); }, function() { console.log('C'); } ]; function usingSwitch(c) { switch (c) { case 0: console.log('A'); break; case 1: console.log('B'); break; case 2: console.log('C'); break; } }
Tests:
Switch
usingSwitch(Math.floor(Math.random() * 3))
Object Literal
objLiteral[Math.floor(Math.random() * 3)]();
Array Literal
arrayLiteral[Math.floor(Math.random() * 3)]();
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Switch
Object Literal
Array Literal
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
Switch
181663.1 Ops/sec
Object Literal
177140.7 Ops/sec
Array Literal
171827.2 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the provided JSON and explore what's being tested, compared, and their pros and cons. **Benchmark Definition** The benchmark is comparing three approaches to call functions with different syntax: 1. **Switch statement**: `usingSwitch(Math.floor(Math.random() * 3))` 2. **Object Literal**: `objLiteral[Math.floor(Math.random() * 3)]();` 3. **Array Literal**: `arrayLiteral[Math.floor(Math.random() * 3)]();` **Library and Purpose** The benchmark uses two libraries: 1. **`console.log`**: a built-in function in JavaScript that outputs the specified value to the console. 2. **`Math.random()`**: a built-in function in JavaScript that generates a random number between 0 (inclusive) and 1 (exclusive). **Special JS Feature/Syntax** The benchmark uses the `switch` statement, which is a control structure that allows execution of code blocks based on conditions. In this case, it's used to call functions with different syntax. Now, let's compare the three approaches: **Switch Statement** Pros: * Can be more readable and maintainable for simple cases * Allows for easy addition of new cases Cons: * Can lead to a long chain of `case` statements, making the code harder to read and maintain * May not be suitable for large numbers of cases **Object Literal** Pros: * Can be more concise and expressive than switch statements * Allows for easy access to properties using bracket notation (e.g., `objLiteral[0]()`) Cons: * Can be less readable and maintainable for complex cases * Requires knowledge of the object's structure to access properties **Array Literal** Pros: * Can be more concise and expressive than switch statements * Allows for easy access to functions using bracket notation (e.g., `arrayLiteral[0]()`) Cons: * May not be suitable for large numbers of functions * Requires knowledge of the array's structure to access functions **Other Considerations** * **Performance**: The benchmark measures execution speed, which is important for performance-critical code. * **Code Readability**: The readability of each approach can impact maintainability and debuggability. Now that we've explored the different approaches, let's look at the benchmark results: The current benchmark result shows that **Object Literal** has a higher execution speed (381604.90625 executions/second) compared to **Switch Statement** (373330.875 executions/second) and **Array Literal** (367839.1875 executions/second). Keep in mind that these results may vary depending on the specific use case, JavaScript engine, and hardware. As an alternative to this benchmark, you could consider using a different approach, such as: * **Function Pointer**: Instead of using `switch` or object/literal notation, use function pointers (e.g., `function* foo() { ... }`) to call functions. * **Polymorphism**: Use polymorphism techniques (e.g., inheritance) to achieve similar results without resorting to switch statements or object literals. These alternatives may offer different trade-offs in terms of readability, maintainability, and performance.
Related benchmarks:
Array.prototype.push vs Array.prototype.push.apply
Array.prototype.push vs Array.prototype.push.apply1
Array.prototype.push vs Array.prototype.push.apply2
Array isArray vs Object.prototype
Switch/case vs indexOf
Comments
Confirm delete:
Do you really want to delete benchmark?