Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
set.has vs. array.includes (100 items)
(version: 0)
Comparing performance of:
includes vs lookup
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var a = new Array(100).fill(0).map((x, i) => i) var b = new Set(a)
Tests:
includes
return a.includes(9)
lookup
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
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):
**What is being tested?** On the provided JSON, two individual test cases are defined: `includes` and `lookup`. These test cases aim to measure the performance of checking if an element exists in an array using different approaches. The first test case, "includes", tests the performance of the `Array.prototype.includes()` method. This method checks if a specified value (in this case, the number 9) is present within a given array (`a`). The second test case, "lookup", tests the performance of the `Set.prototype.has()` method, which performs a similar check but on a Set data structure (`b`). **Options compared:** Two main approaches are being compared: 1. **Array.prototype.includes()**: This method checks if an element exists in an array by iterating over the array's elements and verifying if the specified value matches any of them. 2. **Set.prototype.has()**: This method uses a hash table data structure to store and look up values efficiently, allowing for fast membership testing. **Pros and Cons:** * **Array.prototype.includes():** * Pros: * Widely supported in modern browsers * Can be used with arrays of any type (not just numbers) * Cons: * May have slower performance due to the iteration process, especially for large arrays * Can lead to unnecessary computations if the array is not modified during the lookup * **Set.prototype.has():** * Pros: * Fast membership testing with an average time complexity of O(1) * Efficient use of memory for large datasets * Cons: * Not all browsers support Set data structures, although they have become increasingly popular in recent years **Library and Purpose:** The `Set` library is used here to create a set (`b`) from the array (`a`). The purpose of this library is to store unique values and provide efficient membership testing using the `has()` method. **Special JS Feature or Syntax:** There is no specific JavaScript feature or syntax mentioned in the benchmark definition. However, it's worth noting that the use of Set data structures is a relatively recent addition to JavaScript and has gained popularity due to its efficiency in certain scenarios. **Other Alternatives:** If you were to implement this benchmark without using existing libraries, you could explore other alternatives for membership testing: * **Array.prototype.indexOf()**: This method returns the index of the first occurrence of the specified value within the array. If not found, it returns -1. * **Boolean checks with array indices**: You can use Boolean checks (`if (i === 9)` or `if (!a.includes(9))`) to check for membership in an array. * **Using a Map data structure**: Similar to Sets, Maps store key-value pairs and provide efficient lookup using the `has()` method. However, these alternatives might have varying performance characteristics compared to the `includes()` and `has()` methods, depending on the specific use case and JavaScript environment.
Related benchmarks:
convert to set + set.has vs. array.includes
set.has vs. array.includes 2
has vs includes
Includes (array) vs Has (Set)
array.includes vs. set.has on the fly
Comments
Confirm delete:
Do you really want to delete benchmark?