Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
array.includes vs array.indexOf vs set.has vs weakSet.has
(version: 0)
Comparing performance of:
array.includes vs array.indexOf vs set.has vs weakSet.has
Created:
2 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var array = [] var set = new Set() var weakSet = new WeakSet() var target = {} let i = 100 while (i--) { array.push({}) set.add({}) weakSet.add({}) } array.push(target) set.add(target) weakSet.add(target)
Tests:
array.includes
array.includes(target)
array.indexOf
array.indexOf(target)
set.has
set.has(target)
weakSet.has
weakSet.has(target)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
array.includes
array.indexOf
set.has
weakSet.has
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
10 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36
Browser/OS:
Chrome 138 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
array.includes
35088812.0 Ops/sec
array.indexOf
35947004.0 Ops/sec
set.has
121388128.0 Ops/sec
weakSet.has
68537480.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON and explain what is being tested. **Benchmark Definition** The benchmark measures the performance of four different methods to check for the presence of an element in various data structures: 1. `array.includes(target)`: checks if the `target` object is present in an array. 2. `array.indexOf(target)`: returns the index of the first occurrence of the `target` object in an array, or -1 if not found. 3. `set.has(target)`: checks if the `target` object is present in a Set data structure. 4. `weakSet.has(target)`: checks if the `target` object is present in a WeakSet data structure. **Options Compared** The benchmark compares the performance of these four methods on arrays and Sets, which are two fundamental data structures in JavaScript. **Pros and Cons of Different Approaches:** 1. **`array.includes(target)`**: This method has a time complexity of O(n), where n is the length of the array. However, it can be optimized to have a linear search by using a hash table (e.g., `Map`) to store the elements of the array. 2. **`array.indexOf(target)`**: This method also has a time complexity of O(n) and requires an exhaustive search for each element in the array. However, it returns the index of the first occurrence, which can be useful in some cases. 3. **`set.has(target)`**: This method has an average time complexity of O(1), making it much faster than `array.includes()` or `array.indexOf()`. Sets are implemented as hash tables, allowing for efficient lookups. 4. **`weakSet.has(target)`**: Similar to a regular Set, but WeakSets store only weak references, which can be useful in cases where the object being stored may not prevent its garbage collection. **Library and Its Purpose** In this benchmark, no external libraries are used. However, if you were to use a library like Lodash or Ramda, you might use functions like `_.includes()` or `R.includes()` to achieve similar results. **Special JS Features or Syntax:** There are no special JavaScript features or syntax explicitly mentioned in the provided code snippets. **Other Alternatives:** For comparing performance, other alternatives include: 1. **Benchmarking libraries**: Such as Benchmark.js, which provides a simple way to create and run benchmarks. 2. **Browser benchmarking tools**: Like Chrome DevTools' Benchmarking panel, which allows you to create and run benchmarks directly in the browser. Keep in mind that these alternatives may have additional features or complexity compared to the provided JSON and benchmark code.
Related benchmarks:
IndexOf vs Includes array of numbers
convert to set + set.has vs. array.includes
array.includes vs. set.has on the fly
set.has (w/ creation) vs. array.includes
set vs array includestratsatsrats
Comments
Confirm delete:
Do you really want to delete benchmark?