Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Switch vs Object Lookup
(version: 0)
Comparing performance of:
Switch vs Object Literal
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var str = 'abc'; str = str.charAt(Math.floor(Math.random() * 3));
Tests:
Switch
switch (str) { case 'a': return 'A'; case 'b': return 'B'; case 'c': return 'C'; }
Object Literal
var objLiteral = { a: 'A', b: 'B', c: 'C' } 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/136.0.0.0 Safari/537.36
Browser/OS:
Chrome 136 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Switch
225870544.0 Ops/sec
Object Literal
219375152.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'd be happy to explain what's being tested in the provided JSON benchmark. **Benchmark Overview** The benchmark compares two approaches for looking up values in an object: using a `switch` statement and using an object literal with bracket notation (`objLiteral[str];`). The goal is to determine which approach is faster. **Switch vs Object Lookup** The `switch` statement is used in the first test case: ```javascript switch (str) { case 'a': return 'A'; case 'b': return 'B'; case 'c': return 'C'; } ``` In this approach, the value of `str` is used as an index to look up a corresponding value in an object. The object contains three cases: one for each possible value of `str`. On the other hand, the second test case uses an object literal with bracket notation: ```javascript var objLiteral = { a: 'A', b: 'B', c: 'C' } objLiteral[str]; ``` Here, the value of `str` is used to index into the `objLiteral` object and retrieve the corresponding value. **Pros and Cons** * **Switch Statement** + Pros: - Can be faster for small numbers of cases due to branch prediction. - Can be more readable when dealing with a small, fixed number of cases. + Cons: - Can be slower for large numbers of cases due to the overhead of the `switch` statement. - May not work well with dynamically generated case values. * **Object Literal** + Pros: - Faster for larger numbers of cases because it avoids the overhead of a `switch` statement. - More flexible and can handle dynamically generated case values. + Cons: - Can be less readable when dealing with a large number of cases. - May not work well if the case value is not a valid key in the object. **Library: None** There are no external libraries used in this benchmark. The test cases only use built-in JavaScript features and data types. **Special JS Feature: Object Literals** The second test case uses an object literal, which is a feature introduced in ECMAScript 1999 (ES-3). An object literal is a way to create objects using the `object` keyword followed by property-value pairs enclosed in curly braces. In this case, the object literal is used with bracket notation (`objLiteral[str];`) to access its properties. **Other Alternatives** If you wanted to test other approaches for looking up values in an object, some alternatives could include: * Using a `for...in` loop to iterate over the object's properties. * Using the `hasOwnProperty()` method to check if a property exists before trying to access it. * Using a `Map` data structure instead of an object literal. However, these approaches may not be as natural or efficient for this specific use case.
Related benchmarks:
Switch vs Object Literal methods
Switch vs Object Literal 24r34rf3rr
Switch vs Object Literal value return
Switch vs Object (Simplified)
Comments
Confirm delete:
Do you really want to delete benchmark?