Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
set.has vs. array.includes (100000)
(version: 0)
Comparing performance of:
includes vs lookup
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var a = Array.from({ length: 100000 }).map((v, i) => i); var b = new Set(a);
Tests:
includes
return a.includes((Math.floor(Math.random() * (99999 - 1)) + 1))
lookup
return b.has((Math.floor(Math.random() * (99999 - 1)) + 1))
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):
Measuring JavaScript performance is an essential part of web development, and tools like MeasureThat.net help us understand how different approaches affect execution speed. Let's dive into the benchmark: **Benchmark Definition** The benchmark compares two methods: `array.includes()` and `set.has()`. The script preparation code generates an array `a` with 100,000 elements, each representing a unique integer from 0 to 99,999. A set `b` is created from this array. **Test Cases** There are two test cases: 1. **includes**: This test case measures the performance of the `array.includes()` method by searching for a random integer between 1 and 99,999 within the array. 2. **lookup**: This test case measures the performance of the `set.has()` method by checking if a random integer is present in the set. **Library** In this benchmark, the `Set` library is used. A Set is a collection of unique values, which provides constant-time operations for adding and searching elements. **Special JavaScript Feature/Syntax** There are no special features or syntax mentioned in the code. **Options Compared** The two options being compared are: 1. **array.includes()**: This method checks if an element is present in the array. It has a time complexity of O(n), where n is the length of the array. 2. **set.has()**: This method checks if an element is present in the set. It has a time complexity of O(1), making it much faster than `array.includes()` for large datasets. **Pros and Cons** * **array.includes():** + Pros: Easy to use, widely supported. + Cons: Slow for large datasets (O(n) time complexity). * **set.has():** + Pros: Fast for large datasets (O(1) time complexity), efficient for unique values. + Cons: Requires a Set library, may not be as intuitive. **Other Considerations** When deciding between `array.includes()` and `set.has()`, consider the following: * Use `array.includes()` when: + You need to search for an element in an array that may contain duplicates or has a small number of unique values. + You prioritize code readability and simplicity over performance. * Use `set.has()` when: + You need to perform fast lookups on a large dataset with unique values. + You're willing to invest time in understanding the Set library and its trade-offs. **Alternatives** Other alternatives to `array.includes()` include: 1. **Array.prototype.indexOf()**: Similar to `includes()`, but returns the index of the first occurrence instead of a boolean value. 2. **Array.prototype.findIndex()**: Returns the index of the first element that satisfies a given condition, similar to `includes()`. 3. **Using a different data structure**, such as a binary search tree or a hash table, for efficient lookups. These alternatives may offer better performance than `array.includes()` but often require more code and may not be as widely supported.
Related benchmarks:
set.has vs. array.includes 2
has vs includes
set.has vs. array.includes (find 999,999 in 1,000,000)
Array.includes vs Set.has vas Map.has 2
Array.includes vs Set.has vas Map.has with large data set
Comments
Confirm delete:
Do you really want to delete benchmark?