Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array.Includes vs a few strict equals
(version: 0)
Comparing performance of:
includes #1 vs Equals #1 vs includes #2 vs Equals #2 vs includes #3 vs Equals #3
Created:
6 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var layout = '1', layout2 = '2', layout3 = '3'; var selectedLayout = '1';
Tests:
includes #1
if ([layout, layout2].includes(selectedLayout)) { }
Equals #1
if (selectedLayout === layout || selectedLayout === layout2) { }
includes #2
if ([layout2, layout].includes(selectedLayout)) { }
Equals #2
if (selectedLayout === layout2 || selectedLayout === layout) { }
includes #3
if ([layout3, layout2, layout].includes(selectedLayout)) { }
Equals #3
if (selectedLayout === layout3 || selectedLayout === layout2 || selectedLayout === layout) { }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (6)
Previous results
Fork
Test case name
Result
includes #1
Equals #1
includes #2
Equals #2
includes #3
Equals #3
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 benchmark and its test cases. **Benchmark Definition** The benchmark compares the performance of three different approaches to check if a certain value exists in an array: 1. `includes()`: This method checks if an array includes a specified value, returning `true` if it does, and `false` otherwise. 2. Strict equality (`===`) with individual elements: This approach checks if the specified value is equal to one of the elements in the array individually (e.g., `layout === selectedLayout`). 3. Strict equality (`===`) with an array subset: This approach checks if the specified value exists in a subset of the array, specifically the first two elements (`[layout2, layout]`). **Test Cases** The benchmark consists of four test cases: 1. `includes #1`: Tests the performance of using `includes()` to check for existence. 2. `Equals #1`: Tests the performance of individual element equality checks (e.g., `layout === selectedLayout`). 3. `includes #2`: Tests the performance of using `includes()` with a different array order (`[layout, layout2]`). 4. `Equals #2`: Tests the performance of individual element equality checks with a subset of elements (`selectedLayout === layout || selectedLayout === layout2`). **Options Compared** The benchmark compares two main options: 1. **Array includes vs strict equality**: This option tests whether using `includes()` is faster or slower than performing individual element equality checks. 2. **Strict equality order matters**: This option tests whether the order in which elements are compared affects performance. **Pros and Cons of Each Approach** **Array includes:** * Pros: + Simple to implement + Efficient for large arrays (since it only needs to find one match) * Cons: + May be slower than individual element equality checks for smaller arrays due to overhead **Individual Element Equality Checks:** * Pros: + Faster for smaller arrays or when exact matches are required * Cons: + Requires more code and can be slower for large arrays (since each element needs to be checked individually) **Strict Equality with Array Subset:** * Pros: + Can be faster than individual element equality checks for large arrays, since it only checks a subset of elements * Cons: + May not work as expected if the subset is empty or contains non-existent elements **Library Used** None mentioned in this benchmark. **Special JS Features/Syntax** There are no specific special JavaScript features or syntax used in this benchmark. The tests rely on standard array and equality operators. **Alternatives** Other alternatives to the benchmarked approaches could include: * Using `some()` instead of `includes()` * Employing a different data structure, such as a Set, for faster lookup * Utilizing a library like Lodash or Ramda for optimized array operations
Related benchmarks:
Array.includes() vs Array.indexOf()
IndexOf vs Includes array of numbers
equality vs includes
=== vs includes
equals vs includes
Comments
Confirm delete:
Do you really want to delete benchmark?