Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Unique array (indexOf) vs Set add
(version: 0)
Comparing performance of:
Array Unique vs Set Unique
Created:
5 years ago
by:
Guest
Jump to the latest result
Tests:
Array Unique
let counter = 1e5; const arr = []; while(counter--) { for (let i=0; i< 30; i++) { if (arr.indexOf(i) !== -1) { arr.push(i); } } } arr
Set Unique
let counter = 1e5; const set = new Set(); while(counter--) { for (let i=0; i < 30; i++) { set.add(i); } } [...set];
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Array Unique
Set Unique
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):
**Benchmark Explanation** The provided JSON represents two microbenchmarks that compare the performance of JavaScript arrays with `indexOf` method and sets with `add` method. **Options Compared:** 1. **Array with `indexOf` method**: In this approach, an array is created and populated with 30 unique elements using a while loop. The `indexOf` method is used to check if each element already exists in the array. 2. **Set with `add` method**: In this approach, a Set data structure is created and populated with 30 unique elements using a for loop. The `add` method is used to add each element to the set. **Pros and Cons:** * **Array with `indexOf` method**: * Pros: + Easier to implement, as it uses an array data structure. + More intuitive for developers familiar with JavaScript arrays. * Cons: + The `indexOf` method is O(n) in the worst case, meaning its performance degrades linearly with the size of the array. This can lead to slower execution times for large datasets. * **Set with `add` method**: + Pros: - The `add` method is O(1) on average, making it much faster than the `indexOf` method for large datasets. - Sets are designed to store unique elements, reducing the likelihood of collisions. * Cons: - Requires creating a new Set data structure, which may incur some overhead. - Less intuitive for developers unfamiliar with sets. **Library and Syntax Used:** Neither of these benchmarks uses any external libraries. They only rely on built-in JavaScript features. **Special JS Feature or Syntax:** No special JavaScript feature or syntax is used in these benchmarks. The code follows standard JavaScript syntax and conventions. **Other Alternatives:** 1. **Using `Map` data structure**: Instead of arrays, you could use a Map data structure to store the elements. Maps are designed for storing key-value pairs and can provide faster lookups than arrays with the `indexOf` method. 2. **Parallelizing loops**: You could parallelize the loop using Web Workers or other techniques to further improve performance. However, this would require additional setup and infrastructure. Here's a sample implementation of the two benchmarks in JavaScript: ```javascript // Array with indexOf method function arrayBenchmark() { const counter = 1e5; const arr = []; while (counter--) { for (let i = 0; i < 30; i++) { if (arr.indexOf(i) !== -1) { // do nothing } else { arr.push(i); } } } } // Set with add method function setBenchmark() { const counter = 1e5; const set = new Set(); while (counter--) { for (let i = 0; i < 30; i++) { set.add(i); } } return [...set]; } ``` Note that the `setBenchmark` function returns an array from the set, as the benchmark is designed to compare the two approaches.
Related benchmarks:
lodash uniq vs deconstructed set
set vs array find if exists
set vs array find if exists v2
JS fastest unique array Set vs uniq vs filter
lodash@4.17.21 uniq vs set vs custom unique
Comments
Confirm delete:
Do you really want to delete benchmark?