Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
set.has vs. array.includes vs array.indexOf (small set of number values)
(version: 0)
Numbers instead of strings
Comparing performance of:
includes vs lookup vs indexof
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var a = [ 9, 2, 5, 6, ]; var b = new Set(a)
Tests:
includes
return a.includes(5)
lookup
return b.has(5)
indexof
return a.indexOf(5) >= 0
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
includes
lookup
indexof
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 what's being tested in the provided benchmark. **Benchmark Definition** The benchmark is designed to compare the performance of three different approaches for checking if an element exists in an array or a Set: 1. `array.includes(element)` 2. `Set.has(element)` 3. `array.indexOf(element) >= 0` These approaches are used to find out whether a specific value (`5`) exists in the array or Set created from the array. **Options Compared** The benchmark compares three different options for checking if an element exists in an array: 1. `array.includes(element)`: This method returns a boolean value indicating whether the element is present in the array. 2. `Set.has(element)`: This method returns a boolean value indicating whether the element is present in the Set. 3. `array.indexOf(element) >= 0`: This approach uses the `indexOf` method to find the index of the element, and then checks if it's greater than or equal to 0 (i.e., the element exists). **Pros and Cons** Here are some pros and cons of each approach: 1. `array.includes(element)`: * Pros: Simple, straightforward, and widely supported. * Cons: May be slower for large arrays due to the linear search algorithm used under the hood. 2. `Set.has(element)`: * Pros: Fastest approach, as Sets use a hash table to store elements and provide O(1) lookup time on average. * Cons: Requires creating a Set object from the array, which may incur additional overhead for small arrays or objects with many duplicate values. 3. `array.indexOf(element) >= 0`: * Pros: Suitable for cases where you need to find both the index and existence of an element. * Cons: Slower than `Set.has(element)` due to the linear search algorithm used in `indexOf`, especially for large arrays. **Library and Its Purpose** In this benchmark, the library used is not explicitly mentioned. However, it's likely that the `Set` data structure from the ECMAScript standard is being used, which provides a way to store unique values in a data structure with fast lookup times. **Special JS Features or Syntax** There are no special JavaScript features or syntaxes used in this benchmark. The code only uses standard JavaScript features and syntax for arrays and Sets. **Other Alternatives** For checking if an element exists in an array, other alternatives to `array.includes(element)` might include: * Using a linear search algorithm with a pointer-based approach. * Using a more advanced data structure like a trie or a suffix tree. * Using a third-party library optimized for array lookup, such as `fast-est-improved-array-find` (FEIF). For checking if an element exists in a Set, other alternatives to `Set.has(element)` might include: * Using a different implementation of the Set data structure that prioritizes performance over memory usage. * Using a different algorithm for membership testing, such as Bloom filters or hash tables. Keep in mind that these alternatives would likely require additional overhead and may not be suitable for all use cases.
Related benchmarks:
convert to set + set.has vs. array.includes
set vs array find if exists
set vs array find if exists v2
Array includes vs Set.has
set.has (w/ creation) vs. array.includes
Comments
Confirm delete:
Do you really want to delete benchmark?