Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
set.has vs. array.includes better
(version: 0)
Comparing performance of:
includes vs lookup
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var length = 10000; var arr = [] for (var i = 0; i < length; i++) { arr.push(Math.random()); } var arrset = new Set(arr) var f = arr[arr.length / 2]
Tests:
includes
return arr.includes(f)
lookup
return arrset.has(f)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
includes
lookup
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 benchmark and explain what is being tested, compared, and their pros and cons. **What is being tested?** The benchmark compares two approaches: 1. **`arr.includes(f)`**: This approach uses the `includes()` method to check if a value (`f`) exists in an array (`arr`). The includes() method checks for membership in an array. 2. **`arrset.has(f)`**: This approach uses the `has()` method of a Set object (`arrset`) to check if a value (`f`) is present. **Options compared** The two approaches are being tested to determine which one performs better. **Pros and Cons:** 1. **`arr.includes(f)`**: * Pros: + Widely supported in modern browsers. + Easy to understand and use. * Cons: + May be slower than the `has()` method for large datasets due to its iterative nature. 2. **`arrset.has(f)`**: * Pros: + Generally faster than the `includes()` method for large datasets, as it uses a hash table data structure under the hood. * Cons: + Requires the creation of a Set object from the array, which can be memory-intensive for large datasets. + May not work in older browsers that do not support Set objects. **Other considerations:** 1. **Array iteration**: The `includes()` method iterates over the array to find the value, while the `has()` method uses a hash table lookup. This means that if the array is very large, the `has()` method may be faster. 2. **Memory usage**: Creating a Set object from an array can consume more memory than using the `includes()` method, especially for large datasets. **Library or feature:** The benchmark does not use any specific library or syntax beyond what's already part of JavaScript (ES6+). Now, let's take a look at some alternative approaches: 1. **`arr.indexOf(f)`**: This approach uses the `indexOf()` method to find the index of the value in the array. If the value is not found, it returns -1. 2. **`Array.prototype.findIndex()`**: This approach uses the `findIndex()` method to find the index of the value in the array. If no element matches the provided callback, it returns -1. While these alternatives exist, they are not being tested in this benchmark. Keep in mind that the performance difference between these approaches may vary depending on the specific use case and the size of the dataset.
Related benchmarks:
Fill array with random integers
Array .push() vs .unshift() with random numbers
Set.prototype.has() vs "in" operator
array.from vs spread with set
Array push or set
Comments
Confirm delete:
Do you really want to delete benchmark?