Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Switch vs Object
(version: 0)
Comparing performance of:
switch vs Object
Created:
6 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var icon = 'User';
Tests:
switch
switch (icon) { case 'ArrorUp': icon = ['far', 'chevron-up']; break; case 'ArrorDonw': icon = ['far', 'chevron-down']; break; case 'Camera': icon = ['fas', 'camera']; break; case 'Calendar': icon = ['far', 'calendar-alt']; break; case 'Check': icon = ['far', 'check']; break; case 'Clear': icon = ['far', 'times']; break; case 'Error': icon = ['far', 'exclamation-circle']; break; case 'ExchangeAlt': icon = ['far', 'exchange-alt']; break; case 'Globe': icon = ['far', 'globe']; break; case 'History': icon = ['far', 'history']; break; case 'Info': icon = ['far', 'info-circle']; break; case 'Lock': icon = ['far', 'lock']; break; case 'Pen': icon = ['far', 'pen']; break; case 'Paperclip': icon = ['far', 'paperclip']; break; case 'PlusCircle': icon = ['fas', 'plus-circle']; break; case 'PowerOff': icon = ['far', 'power-off']; break; case 'Search': icon = ['far', 'search']; break; case 'StickyNote': icon = ['far', 'sticky-note']; break; case 'Trash': icon = ['far', 'trash-alt']; break; case 'User': icon = ['fas', 'user-alt']; break; }
Object
var objLiteral = { 'ArrorUp': ['far', 'chevron-up'], 'ArrorDonw': ['far', 'chevron-down'], 'Camera': ['fas', 'camera'], 'Calendar': ['far', 'calendar-alt'], 'Check': ['far', 'check'], 'Clear': ['far', 'times'], 'Error': ['far', 'exclamation-circle'], 'ExchangeAlt': ['far', 'exchange-alt'], 'Globe': ['far', 'globe'], 'History': ['far', 'history'], 'Info': ['far', 'info-circle'], 'Lock': ['far', 'lock'], 'Pen': ['far', 'pen'], 'Paperclip': ['far', 'paperclip'], 'PlusCircle': ['fas', 'plus-circle'], 'PowerOff': ['far', 'power-off'], 'Search': ['far', 'search'], 'StickyNote': ['far', 'sticky-note'], 'Trash': ['far', 'trash-alt'], 'User': ['fas', 'user-alt'], } objLiteral[icon];
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
switch
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):
Let's break down the provided benchmark and explain what's being tested, compared, and their pros and cons. **Benchmark Definition** The benchmark is defined by two test cases: `switch` and `Object`. The script preparation code for both tests is identical, assigning the string `'User'` to a variable named `icon`. **Test Cases** #### 1. switch This test case uses a JavaScript `switch` statement with multiple cases. ```javascript switch (icon) { // ... } ``` The benchmark definition provides the entire `switch` statement as a single string, which is then executed by MeasureThat.net. **Library: none** This test case doesn't use any external libraries. **Special JS feature: None** The `switch` statement is a standard JavaScript construct and doesn't rely on any special features or syntax. **Comparison with Object** The benchmark compares the performance of the `switch` statement with an equivalent approach using an object literal. ```javascript var objLiteral = { 'ArrorUp': ['far', 'chevron-up'], 'ArrorDonw': ['far', 'chevron-down'], // ... }; objLiteral[icon]; ``` **Pros and Cons of switch vs Object** * **switch**: * Pros: Generally faster and more efficient, especially for small numbers of cases. The JavaScript engine can optimize the branch prediction and cache accesses. * Cons: Can be slower for large numbers of cases due to the overhead of creating a jump table. Additionally, it may not work well with dynamically generated case values. * **Object**: * Pros: Allows for dynamic case values and can be more flexible than `switch` when dealing with unknown or variable-case scenarios. It also avoids some issues related to branch prediction and cache misses in modern CPUs. * Cons: Typically slower than `switch`, as the JavaScript engine needs to perform a string lookup and indexing operation. **Other Alternatives** * **Table-driven approaches**: Instead of using `switch` or an object literal, you could use a table-driven approach with arrays or maps. This can provide better performance for large numbers of cases but may require more code and complex logic. * **Runtime-generated switch tables**: If the number of cases is known at runtime, you can generate a jump table dynamically using `switch` statements. However, if the cases are unknown or variable at compile time, this approach may not be feasible. The benchmark result shows that both approaches have different performance characteristics, with `switch` generally outperforming `Object`. The exact results depend on various factors, including the specific JavaScript engine, CPU architecture, and operating system.
Related benchmarks:
Dot property set notation VS Lodash.set
Dot property set notation VS Lodash.set poop
property assign VS Lodash.set
Tom test
Switch/case vs indexOf
Comments
Confirm delete:
Do you really want to delete benchmark?