Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
set.has vs. array.includes - large array
(version: 0)
Comparing performance of:
includes vs lookup
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var a = []; for(let i = 0; i < 1000; i++){a.push(i)} var b = new Set(a)
Tests:
includes
return a.includes(499)
lookup
return b.has(499)
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 definition and test cases. **Benchmark Definition Json:** The benchmark is designed to compare the performance of two approaches: 1. `set.has`: This method checks if an element exists in a Set data structure. 2. `array.includes`: This method checks if an element exists in an array using the `includes` method. The benchmark creates a large array and converts it to a Set, then performs the lookups for both methods. **Script Preparation Code:** ```javascript var a = []; for(let i = 0; i < 1000; i++){ a.push(i); } var b = new Set(a); ``` This code creates an array `a` with 1000 elements and converts it to a Set `b`. **Html Preparation Code:** There is no HTML preparation code provided. **Individual Test Cases:** The benchmark consists of two test cases: 1. `includes`: This test case checks if the element `499` exists in the array using the `array.includes` method. 2. `lookup`: This test case checks if the element `499` exists in the Set using the `set.has` method. **Pros and Cons:** * `array.includes`: + Pros: Fast, efficient, and widely supported. + Cons: Can be slower for very large datasets due to the need to iterate through the array. * `set.has`: + Pros: Fast, efficient, and optimized for Set lookups. + Cons: May not work as expected if the element is not a hashable value. **Library and Purpose:** The `Set` data structure is a built-in JavaScript object that provides an efficient way to store unique values. It uses a hash table internally, which allows for fast lookup operations. **Special JS Feature or Syntax:** There are no special JavaScript features or syntax used in this benchmark. **Other Alternatives:** If you want to compare the performance of `array.includes` with other approaches, you can consider using: * `indexOf()`: This method searches for an element in an array and returns its index. It can be slower than `includes` due to the need to iterate through the array. * `filter()`: This method creates a new array that includes only elements that pass a test. While it's not a direct alternative, it can be used to implement a lookup-like behavior. * Native array methods like `findIndex()` or `some()`: These methods provide more efficient lookup options than `includes`, but may have different requirements and usage patterns. Keep in mind that the performance of these alternatives will vary depending on the specific use case and dataset.
Related benchmarks:
convert to set + set.has vs. array.includes
set.has vs. array.includes large 1
set.has vs. array.includes large 3
set.has vs. array.includes (1 million entries)
Comments
Confirm delete:
Do you really want to delete benchmark?