Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array Exists vs If...else
(version: 0)
Comparing performance of:
If..else vs Array
Created:
6 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<!-- Valdeir Psr | StackOverflow PT #448258 -->
Tests:
If..else
const cidadeFilial = 'GUARUJA'; if(cidadeFilial == 'SAO PAULO'){return 'Filial01';} else if(cidadeFilial == 'RIO DE JANEIRO'){return 'Filial02';} else if(cidadeFilial == 'CURITIBA'){return 'Filial03';} else if(cidadeFilial == 'FLORIANOPOLIS'){return 'Filial03';} else if(cidadeFilial == 'SANTOS'){return 'Filial04';} else if(cidadeFilial == 'GUARUJA'){return 'Filial05';} else if(cidadeFilial == 'BELO HORIZONTE'){return 'Filial05';} else {return 'Central';}
Array
const cidadeFilial = 'GUARUJA'; const filiais = { 'SAO PAULO': 'Filial01', 'RIO DE JANEIRO': 'Filial02', 'CURITIBA': 'Filial03', 'FLORIANOPOLIS': 'Filial03', 'SANTOS': 'Filial04', 'GUARUJA': 'Filial05', 'BELO HORIZONTE': 'Filial05' } return filiais[cidadeFilial] ?? 'Central'
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
If..else
Array
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 dive into the explanation of the provided benchmark. **What is being tested?** The benchmark measures the performance difference between two approaches: 1. **If-else statement**: A classic conditional statement that checks if a condition is true and executes a corresponding block of code if it is. 2. **Array lookup**: A method that uses an array to store values and looks up a specific value in the array. **Options compared** The benchmark compares the performance of two options: 1. **If-else statement**: This option checks for exact matches between the `cidadeFilial` variable and the keys in the `filiais` object. 2. **Array lookup**: This option uses the `??` operator to look up the value associated with `cidadeFilial` in the `filiais` object. **Pros and cons of each approach** 1. **If-else statement** * Pros: + Easy to understand and maintain + Can handle non-existent values (returns 'Central' in this case) * Cons: + May be slower due to the overhead of checking multiple conditions + Requires explicit key-matching 2. **Array lookup** * Pros: + Efficient for large datasets, as it uses a single operation to look up values + Allows for easy addition or removal of keys without modifying the code * Cons: + May be slower due to the overhead of looking up values in an array + Requires understanding of the `??` operator and its behavior **Library and special JS feature** In this benchmark, the `??` operator is used. This is a modern JavaScript feature called the "Nullish Coalescing Operator" (?:), which was introduced in ECMAScript 2020. It allows you to provide a default value when looking up a key in an object that may not exist. **Other considerations** * The benchmark uses a simple, test-driven approach to measure performance differences between the two options. * The `filiais` object is defined statically, which means its contents are known at compile-time. This can impact performance compared to dynamically loaded data. * The use of `const` and `let` declarations ensures that variables are not reassigned or modified unexpectedly. **Alternatives** Other approaches to achieve the same result could include: 1. Using a `switch` statement instead of an if-else chain 2. Utilizing a library like Lodash's `get` function for array lookups 3. Implementing a custom lookup mechanism using a data structure like a hash table or trie However, these alternatives may not provide the same performance benefits as the array lookup approach, and may require additional complexity and overhead.
Related benchmarks:
JS array emptiness check
check array size with and without neg
array.length vs array.length > 0 vs !!array.length
!!array.length vs array.length > 0
array.length vs array.length > 0 vs array.length !== 0
Comments
Confirm delete:
Do you really want to delete benchmark?