Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
set.has vs. array.includes 1million
(version: 0)
Comparing performance of:
includes vs lookup
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var a = Array({ length: 1_000_000 }, (v, index) => index) 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):
Let's dive into the world of JavaScript microbenchmarks on MeasureThat.net. **Benchmark Definition** The provided JSON represents a benchmark that tests the performance of two different approaches: using `array.includes()` and using `Set.has()`. The test case is specifically designed to measure the performance of these two methods when searching for an element in a large array (1 million elements). **Options Compared** Two options are compared: 1. `array.includes()`: This method searches for an element in an array by iterating through each element until it finds a match or reaches the end of the array. 2. `Set.has()`: This method uses a Set data structure to store unique values. When you call `has()` on a Set, it checks if the specified value is present in the Set. **Pros and Cons** Here are some pros and cons of each approach: 1. **array.includes()**: * Pros: Simple, efficient, and widely supported. * Cons: Can be slower for large arrays due to the iteration required. 2. **Set.has()**: * Pros: Faster for large datasets since it uses a data structure optimized for fast lookups. * Cons: Requires an additional object (the Set) to create and maintain, which can be memory-intensive. **Library and Purpose** In this benchmark, the `Set` library is used. A `Set` is a collection of unique values that allows for fast lookups, insertions, and deletions. In this test case, the `Set.has()` method is used to quickly check if an element (9) is present in the Set created from the array. **Special JS Feature or Syntax** There are no special JS features or syntax used in this benchmark other than the use of `Set` which is a standard JavaScript library. **Other Alternatives** If you want to explore alternative approaches, here are some options: 1. **Array.prototype.findIndex()**: This method returns the index of the first element that satisfies the provided condition. It's similar to `includes()` but returns an index instead of a boolean value. 2. **Array.prototype.some()**: This method returns a boolean indicating whether at least one element in the array satisfies the provided condition. 3. **Using a Map**: You could use a `Map` data structure, which is another type of data structure that allows for fast lookups. Keep in mind that these alternatives may have slightly different performance characteristics compared to using `Set.has()` or `array.includes()`.
Related benchmarks:
Includes (array) vs Has (Set)
array.includes vs set.has for small-ish n
array.includes vs set.has for medium n
set vs array find if exists v2
set.has vs. array.includes (find 999,999 in 1,000,000)
Comments
Confirm delete:
Do you really want to delete benchmark?