Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Switch vs Object Literal (loop version)
(version: 0)
Comparing performance of:
Switch vs Object Literal
Created:
3 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var str = 'abc'; str = str.charAt(Math.floor(Math.random() * 3));
Tests:
Switch
for (let i = 0; i++; i < 10000) { switch (str) { case 'a': console.log('A'); break; case 'b': console.log('B'); break; case 'c': console.log('C'); break; } }
Object Literal
const objLiteral = { a: function() { console.log('A'); }, b: function() { console.log('B'); }, c: function() { console.log('C'); } } for (let i = 0; i++; i < 10000) { objLiteral[str](); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Switch
Object Literal
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
8 months ago
)
User agent:
Mozilla/5.0 (Android 14; Mobile; rv:143.0) Gecko/143.0 Firefox/143.0
Browser/OS:
Firefox Mobile 143 on Android
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Switch
138187424.0 Ops/sec
Object Literal
647985920.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks. **What is tested?** The provided JSON benchmark compares two approaches to switch statements in JavaScript: 1. **Switch statement with string literals**: This approach uses a traditional `switch` statement with string literals ('a', 'b', and 'c') inside a loop. 2. **Object literal with function invocation**: This approach uses an object literal (`objLiteral`) with functions (as properties) that take the value of `str` as an argument, and then invokes these functions. **Options compared** The benchmark is comparing two options: * Switch statement with string literals * Object literal with function invocation These two approaches have different performance characteristics, which are being measured by the benchmark. **Pros and Cons** Here's a brief overview of each approach: 1. **Switch statement with string literals** * Pros: + Easy to read and understand. + Can be more efficient in terms of execution time because JavaScript can optimize the switch statement internally. * Cons: + May have performance overhead due to the need to parse and execute multiple cases. 2. **Object literal with function invocation** * Pros: + Can be more concise and flexible, especially when dealing with large numbers of cases or complex logic. * Cons: + May incur performance overhead due to the need to invoke functions dynamically. **Library usage** The benchmark uses a library: `console.log()`. This is a built-in JavaScript function used for logging output to the console. **Special JS features or syntax** There are no special JavaScript features or syntax mentioned in this benchmark. The code only uses standard JavaScript syntax and does not utilize any advanced features like async/await, generators, or decorators. **Other alternatives** If you're interested in exploring other approaches, here are a few options: * **Regular expression-based approach**: Instead of using `switch` statements or object literals, you could use regular expressions to match the value of `str`. This approach would likely be less efficient than the two options being compared. * **Array-based approach**: You could store the values and corresponding actions in an array and iterate through it using a loop. This approach would likely be more efficient than the switch statement or object literal approaches, but may not be as concise. Keep in mind that the choice of approach depends on the specific use case and performance requirements.
Related benchmarks:
Switch vs Object Literal methods
Switch vs Object Literal 24r34rf3rr
Switch vs Object Literal v23023022323
Switch vs Object Literal extended
Switch vs Object (Simplified)
Comments
Confirm delete:
Do you really want to delete benchmark?