Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Set.has vs Array.includes with Small & Large Collections
(version: 0)
Comparing performance of:
Set.has (10 items) vs Array.includes vs Set.has (1,000 items) vs Array.includes (1,000 items)
Created:
4 years ago
by:
Guest
Jump to the latest result
Tests:
Set.has (10 items)
const mySet = new Set([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]); const contains = mySet.has(9);
Array.includes
const myArray = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; const contains = myArray.includes(9) ? true : false;
Set.has (1,000 items)
const myArray = []; for (let i = 0; i < 1000; i++) { myArray.push(i); } const mySet = new Set(myArray); const contains = mySet.has(999);
Array.includes (1,000 items)
const myArray = []; for (let i = 0; i < 1000; i++) { myArray.push(i); } const mySet = new Set(myArray); const contains = myArray.includes(i) ? true : false;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Set.has (10 items)
Array.includes
Set.has (1,000 items)
Array.includes (1,000 items)
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 JSON and explain what's being tested. **Benchmark Definition** The benchmark is testing two approaches to check if an element exists in a collection: 1. `Set.has()` 2. `Array.includes()` **Options Compared** * `Set.has()` vs `Array.includes()`: The primary comparison here. * Small collections (10 items) vs Large collections (1000 items): A secondary comparison, but not the main focus. **Pros and Cons of Each Approach:** 1. **`Set.has()`**: * Pros: + Fast lookups with an average time complexity of O(1). + Can be used for unique values. * Cons: + Not designed for random access or indexing (unlike arrays). 2. **`Array.includes()`**: * Pros: + Provides random access and indexing, making it suitable for large datasets. + Can be used with any type of data, not just unique values. * Cons: + Average time complexity is O(n), which can lead to slower performance for large datasets. **Library Used** In the provided benchmark cases, the `Set` class and the `Array.prototype.includes()` method are used. These are built-in JavaScript constructs that provide efficient data structures and methods for working with collections. **Special JS Feature/Syntax (Not Applicable)** Since there's no special feature or syntax being tested in this benchmark, we can move on to other considerations. **Other Alternatives** For checking if an element exists in a collection: 1. `Array.prototype.some()` or `some()`: This method is similar to `includes()`, but it returns a boolean value indicating whether at least one element matches the condition. 2. `Set.prototype.has()`: Similar to `has()`, but only applicable for Sets. In contrast, if you need random access and indexing: 1. `Array.prototype.indexOf()` or `indexOf()`: This method returns the index of the first occurrence of the specified value in the array. If not found, it returns -1. 2. A custom data structure, like a balanced binary search tree (e.g., AVL tree or red-black tree), which can provide O(log n) lookup efficiency. In summary, `Set.has()` is suitable for fast lookups with unique values, while `Array.includes()` provides random access and indexing for large datasets. The choice ultimately depends on the specific use case and performance requirements.
Related benchmarks:
convert to set + set.has vs. array.includes
array.includes vs set.has for small n
array.includes vs. set.has on the fly
set.has (w/ creation) vs. array.includes
Comments
Confirm delete:
Do you really want to delete benchmark?