Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
set.has vs. array.includes (0)
(version: 0)
Comparing performance of:
includes vs lookup
Created:
2 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var a = Array.from({ length: 100 }, (_, index) => index); var b = new Set(a)
Tests:
includes
return a.includes(69)
lookup
return b.has(69)
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 what's being tested in the provided JSON benchmark. **Benchmark Overview** The benchmark is comparing two different approaches to check if an element exists in an array: `Array.prototype.includes()` and `Set.prototype.has()`. The test case consists of two individual tests: 1. "includes" 2. "lookup" **Script Preparation Code** The script preparation code creates an array `a` with 100 elements, each incrementing from 0 to 99, using the `Array.from()` method and a callback function that returns the current index. A new `Set` object `b` is created by passing the array `a` as its initializer. **Options Compared** The two options being compared are: 1. **Array.prototype.includes()**: This method checks if an element with the specified value (69 in this case) exists in the array. 2. **Set.prototype.has()**: This method checks if a specific element (69 in this case) exists in the set. **Pros and Cons** * `Array.prototype.includes()`: + Pros: Generally faster and more widely supported, as it's a built-in method in most browsers. + Cons: May be slower for very large datasets due to its overhead. * `Set.prototype.has()`: + Pros: Can be faster for certain use cases (e.g., when the set is constructed from an array with unique elements) and provides better performance for membership testing. + Cons: Requires the set to be created, which may add unnecessary overhead. **Library Used** In this benchmark, the `Set` object is used, which is a built-in JavaScript object that allows you to store unique values. The purpose of using a set here is to compare its `has()` method with the `Array.prototype.includes()` method. **Special JS Feature or Syntax (Not Applicable in This Case)** There are no special JavaScript features or syntaxes being tested or utilized in this benchmark. **Other Alternatives** For membership testing in an array, other alternatives include: 1. `indexOf()`: Returns the index of the first occurrence of the specified element. If the element is not found, it returns -1. 2. `forEach()` with a callback function that checks for the presence of the element. 3. `filter()` to create a new array with only the elements that match the condition. For membership testing in a set, alternative approaches include: 1. Using another data structure like an object or an array to store the elements and checking if they exist. 2. Implementing your own custom lookup function using bitwise operations or other optimization techniques. Keep in mind that these alternatives may have different performance characteristics compared to the `Set.prototype.has()` method.
Related benchmarks:
convert to set + set.has vs. array.includes
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?