Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array.includes() vs Set.has() for small array
(version: 0)
Comparing performance of:
Find the second element in array vs Find the second element in set
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
arr = ["test1", "test2"] set = new Set(["test1", "test2"]);
Tests:
Find the second element in array
arr.includes("test2");
Find the second element in set
set.has("test2");
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Find the second element in array
Find the second element in set
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Mobile Safari/537.36
Browser/OS:
Chrome Mobile 124 on Android
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Find the second element in array
7191981.0 Ops/sec
Find the second element in set
7303041.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'd be happy to help explain what's being tested in this JavaScript microbenchmark on MeasureThat.net. **What is being tested?** The benchmark compares two approaches for finding the second element in an array: `includes()` and `Set.has()`. The test cases are designed to measure the performance difference between these two methods. **Options compared:** 1. **`arr.includes('test2')`**: This method uses the `includes()` function to check if a specific string (`'test2'`) is present in the array. 2. **`set.has('test2')`**: This method uses the `Set` data structure and its `has()` function to check if a specific value (`'test2'`) is present in the set. **Pros and Cons:** 1. **`arr.includes('test2')`**: * Pros: + Simple and easy to understand. + Widely supported across browsers. * Cons: + May have performance issues for large arrays, as it uses a linear search algorithm. 2. **`set.has('test2')`**: * Pros: + Generally faster than `includes()` for small to medium-sized datasets. + More efficient use of memory, as sets only store unique values. * Cons: + May not be supported by older browsers or those with limited JavaScript capabilities. + Requires the creation of a `Set` object, which can have additional overhead. **Other considerations:** 1. **Library usage**: The benchmark uses the `Set` library, which is a built-in feature in modern JavaScript and most web browsers. This allows developers to focus on the performance differences between the two methods rather than worrying about compatibility issues. 2. **Special JS features or syntax**: Neither of these test cases uses any special JavaScript features or syntax beyond what's required for the benchmark itself. **Alternatives:** 1. For small arrays, `arr.includes('test2')` might be a more straightforward choice due to its simplicity and widespread support. 2. For larger datasets or performance-critical applications, `set.has('test2')` could be a better option due to its efficiency and speed advantages. Overall, this benchmark provides a useful comparison of two common methods for finding elements in an array, allowing developers to make informed decisions about their code's performance and optimization strategies.
Related benchmarks:
convert to set + set.has vs. array.includes
set vs array lookup
Array includes vs Set.has
set.has (w/ creation) vs. array.includes
Comments
Confirm delete:
Do you really want to delete benchmark?