Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
dict allocation vs if vs switch
(version: 0)
Comparing performance of:
dict vs if vs switch
Created:
3 years ago
by:
Guest
Jump to the latest result
Tests:
dict
const dict = {}; dict['test111111111111111111111111111111111111111111111111111111111'] = 'sth1'; dict['test222222222222222222222222222222222222222222222222222222222'] = 'sth2'; dict['test333333333333333333333333333333333333333333333333333333333'] = 'sth3'; let t = 'nn'; console.log(dict[t]); t='test111111111111111111111111111111111111111111111111111111111'; console.log(dict[t]); t='test222222222222222222222222222222222222222222222222222222222'; console.log(dict[t]); t='test333333333333333333333333333333333333333333333333333333333'; console.log(dict[t]);
if
let t = 'nn'; if(t === 'test111111111111111111111111111111111111111111111111111111111') { console.log('sth1'); } else if(t == 'test222222222222222222222222222222222222222222222222222222222') { console.log('sth2'); } else if(t == 'test333333333333333333333333333333333333333333333333333333333') { console.log('sth3'); } else { console.log(undefined); } t='test111111111111111111111111111111111111111111111111111111111'; if(t === 'test111111111111111111111111111111111111111111111111111111111') { console.log('sth1'); } else if(t == 'test222222222222222222222222222222222222222222222222222222222') { console.log('sth2'); } else if(t == 'test333333333333333333333333333333333333333333333333333333333') { console.log('sth3'); } else { console.log(undefined); } t='test222222222222222222222222222222222222222222222222222222222'; if(t === 'test111111111111111111111111111111111111111111111111111111111') { console.log('sth1'); } else if(t == 'test222222222222222222222222222222222222222222222222222222222') { console.log('sth2'); } else if(t == 'test333333333333333333333333333333333333333333333333333333333') { console.log('sth3'); } else { console.log(undefined); } t='test333333333333333333333333333333333333333333333333333333333'; if(t === 'test111111111111111111111111111111111111111111111111111111111') { console.log('sth1'); } else if(t == 'test222222222222222222222222222222222222222222222222222222222') { console.log('sth2'); } else if(t == 'test333333333333333333333333333333333333333333333333333333333') { console.log('sth3'); } else { console.log(undefined); }
switch
let t='nn'; switch(t) { case 'test111111111111111111111111111111111111111111111111111111111': console.log('sth1'); break; case 'test222222222222222222222222222222222222222222222222222222222': console.log('sth2'); break; case 'test333333333333333333333333333333333333333333333333333333333': console.log('sth3'); break; default: console.log(undefined); } t='test1'; switch(t) { case 'test111111111111111111111111111111111111111111111111111111111': console.log('sth1'); break; case 'test222222222222222222222222222222222222222222222222222222222': console.log('sth2'); break; case 'test333333333333333333333333333333333333333333333333333333333': console.log('sth3'); break; default: console.log(undefined); } t='test2'; switch(t) { case 'test111111111111111111111111111111111111111111111111111111111': console.log('sth1'); break; case 'test222222222222222222222222222222222222222222222222222222222': console.log('sth2'); break; case 'test333333333333333333333333333333333333333333333333333333333': console.log('sth3'); break; default: console.log(undefined); } t='test3'; switch(t) { case 'test111111111111111111111111111111111111111111111111111111111': console.log('sth1'); break; case 'test222222222222222222222222222222222222222222222222222222222': console.log('sth2'); break; case 'test333333333333333333333333333333333333333333333333333333333': console.log('sth3'); break; default: console.log(undefined); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
dict
if
switch
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):
A multi-test benchmark! After analyzing the provided test data, I'll try to deduce some insights about the performance differences between `if` statements and `switch` statements. **Observations:** 1. The two "dict" tests are not directly comparable to the other tests (including `if` and `switch`). They seem to be measuring dictionary lookups, which might have different optimization strategies than conditional statements. 2. The three `if` test cases have similar execution rates (`41986.125`, `40846.41796875`, and `37489.80859375`), indicating that they are relatively fast in this specific Chrome 103 browser configuration. 3. The two "switch" tests, however, have significantly slower execution rates (`40960.208212890625` and `40792.546875`). This suggests that the `switch` statements might be slower than expected or require additional overhead. **Possible reasons:** 1. **Branch prediction**: In some cases, the `if` statement's condition is more predictable than a `switch` statement's, leading to better branch prediction and faster execution. 2. **Type of comparison**: The comparisons in `if` statements are typically simple and quick (e.g., comparing integers). In contrast, `switch` statements often involve string comparisons, which can be slower due to the overhead of searching through a table of values. 3. **Code generation**: The browser's just-in-time (JIT) compiler might generate more efficient code for the `if` statement than the `switch` statement. **Conclusion:** While there are some variations in execution rates across these tests, the results suggest that `if` statements generally outperform `switch` statements in this specific Chrome 103 browser configuration. However, it's essential to note that the actual performance differences might be more pronounced in other scenarios or browsers.
Related benchmarks:
Switch vs map with string keys and early exit
switch vs if-else vs lookup
Int switch vs str switch vs int if-else vs str if-else vs int object-key vs str object-key
Compare switch vs dictionary complex 1000
object map vs switch 「!!!!」
Comments
Confirm delete:
Do you really want to delete benchmark?