Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
array.includes vs set.has for medium n
(version: 0)
array.includes vs set.has for medium n
Comparing performance of:
array includes vs set has
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var a = Array.from({ length: 1000 }, (_, i) => i) var b = new Set(a)
Tests:
array includes
return a.includes(490)
set has
return b.has(510)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
array includes
set has
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
10 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 YaBrowser/25.6.0.0 Safari/537.36
Browser/OS:
Yandex Browser 25 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
array includes
15967539.0 Ops/sec
set has
189191376.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark. **What is being tested?** The benchmark is comparing two approaches to check if an element exists in a data structure: 1. `Array.includes()`: This method checks if a specified value exists as an element in an array. 2. `Set.has()`: This method checks if a specified value exists in a set (an unordered collection of unique elements). **Options compared** The benchmark is comparing the performance of these two approaches for medium-sized arrays and sets. **Pros and Cons of each approach:** 1. **Array.includes()** * Pros: + Widely supported and implemented across different browsers and environments. + Can be used on various data structures, not just arrays. * Cons: + Has a linear search complexity, which can lead to slower performance for large datasets. 2. **Set.has()** * Pros: + Provides faster lookup times due to the set's hash table implementation. * Cons: + Requires a set data structure, which may not be available or suitable for all use cases. **Special considerations:** The benchmark does not appear to have any special JavaScript features or syntax, so we'll focus on the standard language. **Library usage** In this case, the `Array.from()` method is used to create an array from a given iterable. This method is part of the ECMAScript 2015 (ES6) specification and is widely supported across modern browsers and environments. **Other alternatives:** For `Set.has()`, alternative approaches might include: 1. Using `indexOf()` on an array. 2. Implementing a custom lookup table or hash function. However, these alternatives may not be as efficient or scalable as the built-in `Set.has()` method. **Benchmark preparation code** The script prepares an array of 1000 elements and creates a set from it using `Array.from()`: ```javascript var a = Array.from({ length: 1000 }, (_, i) => i) var b = new Set(a) ``` This code ensures that the dataset is large enough to demonstrate performance differences between the two approaches. **Individual test cases** The benchmark consists of two individual test cases, each defining a different scenario: 1. `array includes`: Checks if a specific value (490) exists in the array using `Array.includes()`. 2. `set has`: Checks if a specific value (510) exists in the set using `Set.has()`. These test cases demonstrate the performance difference between the two approaches for searching within an unordered collection of unique elements.
Related benchmarks:
convert to set + set.has vs. array.includes
array.includes vs set.has for small n
Includes (array) vs Has (Set)
array.includes vs set.has for small-ish n
Comments
Confirm delete:
Do you really want to delete benchmark?