Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array vs Set loopup in Javascript
(version: 0)
Comparing performance of:
Array.prototype.includes vs Set.prototype.has
Created:
4 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var arr = []; var i = 0; while (i <= 1E5) arr.push(++i); var set = new Set(arr);
Tests:
Array.prototype.includes
const isExits = arr.includes(1E5)
Set.prototype.has
const isExits = set.has(1E5)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Array.prototype.includes
Set.prototype.has
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36
Browser/OS:
Chrome 124 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Array.prototype.includes
26261336.0 Ops/sec
Set.prototype.has
27952500.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark JSON and explain what's being tested. **Benchmark Definition** The benchmark is defined by two separate script preparation codes, which are: 1. A JavaScript code that creates an array `arr` with 100,000 elements using a while loop and pushes each element to the array. 2. A new Set object `set` created from the `arr` array. **Options being compared** The benchmark is comparing two different data structures in JavaScript: Arrays (`Array.prototype.includes`) and Sets (`Set.prototype.has`). Both methods are used to check if an element (in this case, 100,000) exists within the collection. **Pros and Cons of each approach** 1. **Arrays (`Array.prototype.includes`)**: * Pros: + Widely supported across all modern browsers. + Familiar syntax for many developers. + Can be optimized by some JavaScript engines (e.g., V8 in Chrome). * Cons: + May have slower performance compared to Sets for large datasets. + Requires a linear search, which can lead to O(n) time complexity. 2. **Sets (`Set.prototype.has`)**: * Pros: + Generally faster performance than Arrays for large datasets due to its hash-based lookup. + More efficient for checking membership in a collection with unique elements. * Cons: + Not as widely supported across older browsers or versions (not recommended). + Requires the Set object to be created and populated, which adds overhead. **Library usage** In this benchmark, no external libraries are used. However, it's worth noting that in real-world scenarios, you might use libraries like Lodash for utility functions or other libraries for data structures if you're working with complex datasets. **Special JS feature or syntax** There is no special JavaScript feature or syntax being used here, as the benchmark focuses on comparing two standard methods (`includes` and `has`) between Arrays and Sets.
Related benchmarks:
recreate array vs set 2
Create Set vs loop
empty an array in JavaScript - [] vs setting length
Array.push(x) vs array[n]=x
Comments
Confirm delete:
Do you really want to delete benchmark?