Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
convert to set + set.has vs. array.includes
(version: 0)
Check if it is faster to convert an array to a set and perform set.has(), compared to performing array.includes() on the base array.
Comparing performance of:
includes vs convert to set
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
Tests:
includes
return a.includes(9)
convert to set
var b = new Set(a) return b.has(9)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
includes
convert to set
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36
Browser/OS:
Chrome 134 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
includes
89467664.0 Ops/sec
convert to set
7958976.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Overview of the Benchmark** The provided benchmark measures the performance difference between two approaches: using `array.includes()` and converting an array to a set with `set.has()`. This test helps determine which method is faster for this specific use case. **Options Compared** Two options are compared: 1. **Using `array.includes()`**: This approach involves searching for a specific value in the original array. 2. **Converting to set and using `set.has()`**: This approach involves converting the array to a set, which automatically removes duplicates, and then checking if the desired value is present in the set. **Pros and Cons of Each Approach** 1. **Using `array.includes()`:** * Pros: + Simple and straightforward implementation. + Works well for small to medium-sized arrays. * Cons: + May be slower for large arrays due to the need to scan the entire array from scratch. 2. **Converting to set and using `set.has()`**: * Pros: + Can be faster for large arrays, as sets provide an efficient way to check for presence without scanning the entire array. + Removes duplicates automatically, which can be beneficial in certain scenarios. * Cons: + May require additional memory allocation for the set data structure. + May not perform well if the array is very small or has a low number of unique values. **Library Used** The `Set` object is used as part of this benchmark. A `Set` in JavaScript is an unordered collection of unique values, which can be used to store and look up elements efficiently. **Special JS Feature or Syntax** None mentioned in the provided code snippet. However, note that sets are a built-in JavaScript data structure since ECMAScript 5 (released in 2009), so no special features or syntax are required to use them. **Other Considerations** This benchmark measures performance specifically for searching an array for a single value using `includes()` versus converting the array to a set and checking for presence with `has()`. Other factors that might influence the choice of approach include: * Array size and density (number of unique values) * Performance requirements * Memory constraints * Code readability and maintainability **Alternatives** If you need to perform similar searches or operations on arrays, consider exploring other approaches, such as: 1. Using `find()` or `findIndex()` methods with a callback function. 2. Implementing custom search algorithms, like binary search (more suitable for sorted arrays). 3. Utilizing other data structures, like trees or graphs, if they better suit your use case. Keep in mind that the choice of approach depends on your specific requirements and constraints.
Related benchmarks:
Includes (array) vs Has (Set)
array.includes vs. set.has on the fly
Array includes vs Set.has
set.has (w/ creation) vs. array.includes
Comments
Confirm delete:
Do you really want to delete benchmark?