Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Includes VS new Set
(version: 0)
Comparing performance of:
Includes vs new Set
Created:
2 years ago
by:
Guest
Jump to the latest result
Tests:
Includes
const arr1 = Array.from({ length: 1_000_000 }, () => Math.floor(Math.random() * 100_000), ) const arr2 = Array.from({ length: 1_000_000 }, () => Math.floor(Math.random() * 100_000), ) const fn = () => { for (const el of arr1) { if (arr2.includes(el)) return true } } fn()
new Set
const arr1 = Array.from({ length: 1_000_000 }, () => Math.floor(Math.random() * 100_000), ) const arr2 = Array.from({ length: 1_000_000 }, () => Math.floor(Math.random() * 100_000), ) const fn = () => { const set = new Set(arr1) for (const el of arr2) { if (set.has(el)) return true } } fn()
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Includes
new Set
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.2.1 Safari/605.1.15
Browser/OS:
Safari 17 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Includes
4.8 Ops/sec
new Set
4.4 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Benchmark Overview** The provided benchmark, hosted on MeasureThat.net, compares the performance of two approaches to check if an element exists in an array: using `includes()` and creating a new `Set` from the array. **What is being tested?** In this benchmark, we have two test cases: 1. **Includes**: This test case uses the `includes()` method to search for elements in the first array (`arr1`) within the second array (`arr2`). The implementation involves iterating over each element in `arr1` and checking if it exists in `arr2`. 2. **new Set**: In this test case, a new set is created from the elements of `arr1`, and then an element is checked to see if it exists in the set. **Options compared** The two options being compared are: * Using the `includes()` method * Creating a new `Set` from the array These two approaches have different performance characteristics, which are the focus of this benchmark. **Pros and Cons** ### Includes Method Pros: * Widely supported and efficient in modern browsers. * Simple to implement and understand. Cons: * May be slower than creating a set for large arrays due to the iteration and lookup process. ### new Set Pros: * Can be faster than `includes()` for large arrays because it uses a more optimized data structure (a hash table) for lookups. * More efficient when dealing with unique elements, as sets only store unique values. Cons: * Requires creating an additional object from the array, which can add overhead. * May require more memory to store the set, depending on the size of the array. **Library/Technique** The `includes()` method is a built-in JavaScript function that uses a linear search algorithm. The creation of a new `Set` involves converting the array into an object using the `Array.from()` and `Set()` constructors, which creates a hash table for efficient lookups. **Special JS Feature/Syntax** There are no special features or syntaxes used in this benchmark beyond standard JavaScript. **Other Considerations** When choosing between these two approaches, consider the specific requirements of your application: * If you need to check if an element exists in an array frequently, creating a set may be a better choice for performance. * However, if simplicity and readability are more important than raw speed, using `includes()` might be sufficient. **Alternatives** If you're looking for alternative approaches, consider the following: * Using a library like Lodash's `contains` function, which is optimized for performance and uses a similar approach to creating a set. * Implementing your own custom lookup data structure, such as a hash table or trie, for optimal performance. Keep in mind that these alternatives may require more expertise and resources than the built-in `includes()` method.
Related benchmarks:
convert to set + set.has vs. array.includes
new Set([x]).has vs [x].includes
array.includes vs. set.has on the fly
set.has (w/ creation) vs. array.includes
equals vs includes (one value)
Comments
Confirm delete:
Do you really want to delete benchmark?