Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Switch vs Object Literal in javascript
(version: 0)
Comparing performance of: Switch vs Object Literal
Comparing performance of:
Switch vs Object Literal
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var str = 'abcdef'; str = str.charAt(Math.floor(Math.random() * 3));
Tests:
Switch
switch (str) { case 'a': console.log('A'); break; case 'b': console.log('B'); break; case 'c': console.log('C'); break; case 'd': console.log('D'); break; case 'e': console.log('E'); break; case 'f': console.log('F'); break; }
Object Literal
var objLiteral = { a: function() { console.log('A'); }, b: function() { console.log('B'); }, c: function() { console.log('C'); }, d: function() { console.log('D'); }, e: function() { console.log('E'); }, f: function() { console.log('F'); } } 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:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36
Browser/OS:
Chrome 131 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Switch
534599.8 Ops/sec
Object Literal
495240.4 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Benchmark Overview** The provided benchmark compares the performance of two approaches: `switch` statements and object literals in JavaScript. **What is being tested?** In this benchmark, the script preparation code generates a random string `str` with 3 possible characters ('a', 'b', or 'c'). The script then uses either a `switch` statement or an object literal to determine which function to call based on the last character of the string. **Options being compared** The two options being compared are: 1. **Switch Statement**: A traditional `switch` statement is used to check the value of `str` against a set of predefined cases. 2. **Object Literal**: An object literal is used to define a mapping between the characters in `str` and the corresponding functions to call. **Pros and Cons** * **Switch Statement**: + Pros: Can be more readable and maintainable, especially for complex logic. + Cons: May have slower execution due to the overhead of string comparison and case lookup. * **Object Literal**: + Pros: Can be more concise and efficient, especially for simple mappings. + Cons: May require more memory to store the object literal, and can be less readable for complex logic. **Library and Purpose** In this benchmark, there is no specific library being used. However, it's worth noting that some JavaScript engines may optimize certain features or syntax, such as `switch` statements with long chains of cases. **Special JS feature or syntax** There are no special JS features or syntaxes being used in this benchmark other than the standard `switch` statement and object literals. **Other alternatives** Other alternatives to these approaches could include: * Using a `lookupTable` array to store the functions to call based on the character values * Using a regular expression to match the last character of the string against a pattern * Using a function pointer or a closure to encapsulate the logic However, it's worth noting that these alternatives may not provide significant performance improvements over the `switch` statement and object literal approaches. **Benchmark Preparation Code** The provided script preparation code is: ```javascript var str = 'abcdef'; str = str.charAt(Math.floor(Math.random() * 3)); ``` This code generates a random string `str` with 3 possible characters ('a', 'b', or 'c') and assigns it to the variable after generating a new random index. **Individual Test Cases** The two individual test cases are: * **Switch**: Uses a traditional `switch` statement to determine which function to call based on the last character of the string. ```javascript switch (str) { case 'a': console.log('A'); break; case 'b': console.log('B'); break; case 'c': console.log('C'); break; case 'd': console.log('D'); break; case 'e': console.log('E'); break; case 'f': console.log('F'); break; } ``` * **Object Literal**: Uses an object literal to define a mapping between the characters in `str` and the corresponding functions to call. ```javascript var objLiteral = { a: function() { console.log('A'); }, b: function() { console.log('B'); }, c: function() { console.log('C'); }, d: function() { console.log('D'); }, e: function() { console.log('E'); }, f: function() { console.log('F'); } } objLiteral[str](); ```
Related benchmarks:
Switch vs Object Literal w/out console.log 2
Switch vs Object Function vs Object Property
Switch vs Object Literal - SR Test
Switch vs Object Literal in javascript 1
Switch vs Object Literal - testing with stable input
Comments
Confirm delete:
Do you really want to delete benchmark?