Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Switch: String vs Object as expr
(version: 0)
Comparing performance of:
String vs Object
Created:
9 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
function Metric1(name) {this.name = name;} Metric1.TEST1 = new Metric1("TEST1"); Metric1.TEST2 = new Metric1("TEST2"); Metric1.TEST3 = new Metric1("TEST3"); var Metric2 = { TEST1: "TEST1", TEST2: "TEST2", TEST3: "TEST3" };
Tests:
String
var i = 0; for (var j = 0; j < 100; ++j) { var metric = Metric2.TEST3; switch(metric) { case "TEST1": i += 1; break; case "TEST2": i += 2; break; case "TEST3": i += 3; break; } }
Object
var i = 0; for (var j = 0; j < 100; ++j) { var metric = Metric1.TEST3; switch(metric) { case Metric1.TEST1: i += 1; break; case Metric1.TEST2: i += 2; break; case Metric1.TEST3: i += 3; break; } }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
String
Object
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Benchmark Overview** The provided benchmark measures the performance difference between using strings and objects as expressions in a switch statement. The benchmark is designed to test the performance of two different approaches: one that uses string literals (strings) and another that uses object references (objects). **Script Preparation Code** The script preparation code defines two classes, `Metric1` and `Metric2`, which are used to create instances for the benchmark. `Metric1` has three properties (`TEST1`, `TEST2`, and `TEST3`) that contain string values, while `Metric2` is an object with the same property values. **Benchmark Definition** The benchmark definition contains two test cases: 1. **String**: This test case uses a switch statement with string literals as case values. 2. **Object**: This test case uses a switch statement with object references as case values (specifically, the `Metric1` class instances). **Options Compared** Two options are compared in this benchmark: * **Strings** (`"TEST1"`, `"TEST2"`, `"TEST3"`): used as case values in the switch statement. * **Objects** (`Metric1.TEST1`, `Metric1.TEST2`, `Metric1.TEST3`): used as case values in the switch statement. **Pros and Cons** Using strings: * Pros: + Easier to read and maintain code + Faster lookup times (hash table-based lookups) * Cons: + Less flexible (can't dynamically create string literals at runtime) Using objects: * Pros: + More flexible (can dynamically create object references at runtime) + Can store additional data with each case value * Cons: + Slower lookup times (requires hash table-based lookups or explicit comparisons) + More verbose code **Library and Purpose** No specific libraries are used in this benchmark. However, it's worth noting that JavaScript's `switch` statement relies on a mechanism called "switch optimization" to improve performance. This optimization involves looking up the case value in an internal array (if using strings) or a lookup table (if using objects). The choice of using strings or objects as case values can impact this optimization. **Special JS Feature** None mentioned, but it's worth noting that JavaScript's `switch` statement does not support dynamic case values. This means that the cases must be known at compile-time, and any attempt to use a dynamically created value would result in a syntax error. **Other Alternatives** For comparison purposes, other alternatives could include: * Using regular expressions instead of switch statements * Using JavaScript's `switch` statement with array or object literals as case values (e.g., `case [value]`) * Using third-party libraries that provide optimized switch statements for performance-critical code However, these alternatives are not part of the benchmark and would require additional modifications to compare their performance.
Related benchmarks:
JavaScript spread operator vs Object.assign performance 3
Object property assign vs Object.assign performance
JS Object assign vs object new property
JavaScript Object Mutation vs Object.assign performance
Object assign vs regular assignment
Comments
Confirm delete:
Do you really want to delete benchmark?