Toggle navigation
MeasureThat
.net
Create a benchmark
Tools
Feedback
Feature Requests
FAQ
Register
Log In
Switch vs Object Lookup
(version: 0)
Benchmark.js
Comparing performance of:
Switch vs Object Literal
Created:
4 years ago
by:
Guest
Go 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:
2 months ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64; rv:152.0) Gecko/20100101 Firefox/152.0
Browser/OS:
Firefox 152 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Object Literal
2298.8M/s
Switch
1646.7M/s
View exact numbers
Test name
Executions per second
✓
Object Literal
2,298,839,808 Ops/sec
Switch
1,646,745,472 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 (Simplified)
Comments
Confirm delete:
Do you really want to delete benchmark?