Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Switch case vs Object Literal
(version: 0)
Comparing performance of:
Switch vs Object Literal
Created:
2 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var str = 'abcdefghijklmnopqrstuvwxyz'; 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; case 'g': console.log('G'); break; case 'h': console.log('H'); break; case 'i': console.log('I'); break; case 'j': console.log('J'); break; case 'k': console.log('K'); break; case 'l': console.log('L'); break; case 'm': console.log('M'); break; case 'n': console.log('N'); break; case 'o': console.log('O'); break; case 'p': console.log('P'); break; case 'q': console.log('Q'); break; case 'r': console.log('R'); break; case 's': console.log('S'); break; case 't': console.log('T'); break; case 'u': console.log('U'); break; case 'v': console.log('V'); break; case 'w': console.log('W'); break; case 'x': console.log('X'); break; case 'y': console.log('Y'); break; case 'z': console.log('Z'); 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'); }, g: function() { console.log('G'); }, h: function() { console.log('H'); }, i: function() { console.log('I'); }, j: function() { console.log('J'); }, k: function() { console.log('K'); }, l: function() { console.log('L'); }, m: function() { console.log('M'); }, n: function() { console.log('N'); }, o: function() { console.log('O'); }, p: function() { console.log('P'); }, q: function() { console.log('Q'); }, r: function() { console.log('R'); }, s: function() { console.log('S'); }, t: function() { console.log('T'); }, u: function() { console.log('U'); }, v: function() { console.log('V'); }, w: function() { console.log('W'); }, x: function() { console.log('X'); }, y: function() { console.log('Y'); }, z: function() { console.log('Z'); }, } 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:
2 years ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36
Browser/OS:
Chrome 122 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Switch
408104.1 Ops/sec
Object Literal
452190.2 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the provided benchmark definition and test cases. **Benchmark Definition:** The benchmark is designed to compare the performance of two approaches: 1. **Switch statement**: This approach uses a traditional `switch` statement to execute different blocks of code based on the value of a variable (`str`). 2. **Object Literal**: This approach uses an object literal (a JavaScript object created using curly brackets `{}`) with functions as values, and then accesses one of those functions by its key (also `str`). **Benchmark Preparation Code:** The script preparation code sets up a string variable `str` containing all lowercase letters from 'a' to 'z'. The value of `str` is randomly selected from the three letters using `Math.floor(Math.random() * 3)`, which ensures that each letter (and therefore, each branch in the switch statement or object literal access) has an equal chance of being executed. **Individual Test Cases:** 1. **Switch**: This test case uses a traditional `switch` statement to execute different blocks of code based on the value of `str`. The `switch` statement is as follows: ```javascript switch (str) { // ... } ``` In this implementation, each letter from 'a' to 'z' corresponds to a specific block of code that logs the corresponding letter to the console. 2. **Object Literal**: This test case uses an object literal with functions as values, and then accesses one of those functions by its key (also `str`). The object literal is defined as: ```javascript var objLiteral = { // ... }; objLiteral[str](); // access the function corresponding to str ``` In this implementation, each letter from 'a' to 'z' corresponds to a specific function in the object literal that logs the corresponding letter to the console. **Options Compared:** The benchmark compares the performance of two approaches: 1. **Switch statement**: This approach uses a traditional `switch` statement with multiple branches. 2. **Object Literal**: This approach uses an object literal with functions as values and accesses one of those functions by its key. **Pros and Cons:** * **Switch Statement:** + Pros: - Traditional and well-understood syntax - Can be more readable for some developers + Cons: - May lead to slower performance due to the overhead of branching and fallthrough logic - Can be more complex to write and maintain, especially for larger switch statements * **Object Literal:** + Pros: - Can be more concise and expressive than traditional `switch` statements - Can provide a clear and explicit way to map values to functions or other actions + Cons: - May require additional memory allocation for the object literal, which can impact performance - Can be less readable for some developers who are not familiar with object literals **Latest Benchmark Result:** The latest benchmark result shows that the **Object Literal** approach performed better than the traditional `switch` statement approach. However, it's essential to note that this may not always be the case and can depend on various factors such as the specific use case, hardware, and software configuration. In conclusion, the benchmark highlights the trade-offs between using a traditional `switch` statement versus an object literal with functions as values. While both approaches have their pros and cons, the object literal approach demonstrated better performance in this particular benchmark.
Related benchmarks:
Nathan's Switch vs Object Literal Test
Switch vs Object Literal - SR Test
Switch vs Object Literal value return
Switch vs Object Literal bbg
Switch vs Object Literal extended
Comments
Confirm delete:
Do you really want to delete benchmark?