Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Unique array (indexOf) vs Set add 2
(version: 0)
rip not making an account... made a mistake in the first one
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):
Let's break down the provided benchmark data and explain what's being tested. **Benchmark Definition** The benchmark definition is a JSON object that describes two test cases: 1. **Unique array (indexOf) vs Set add 2**: This test case compares the performance of using `Array.indexOf()` to check for uniqueness versus adding elements to an empty array. 2. **Set Unique**: This test case measures the performance of creating a new set and adding elements to it. **Options being compared** The two options being compared are: 1. Using `Array.indexOf()` to check for uniqueness (Array Unique) 2. Adding elements to an empty array to check for uniqueness (Set Unique) **Pros and Cons of each approach:** * **Using `Array.indexOf()`**: This method has a time complexity of O(n) in the worst case, where n is the number of elements being checked. This can be slow for large arrays. Additionally, it creates an extra step that involves searching through the array to find the index of the element. * **Adding elements to an empty array**: This approach creates a new array and adds elements to it directly. The time complexity of this method is O(n), but it's generally faster than `Array.indexOf()` because it avoids the extra search step. **Library used:** The test case "Set Unique" uses a built-in JavaScript `Set` object, which is a data structure that automatically eliminates duplicates and provides fast membership testing. The purpose of using a set is to quickly add elements and check for uniqueness without having to iterate through an array. **Special JS feature or syntax:** There doesn't appear to be any special JavaScript features or syntax being used in these benchmark cases. They are straightforward examples of basic array manipulation and set operations. **Other alternatives:** If you wanted to test alternative approaches, here are a few options: * Using `Array.prototype.includes()` instead of `Array.indexOf()` * Using a data structure like a `Map` or a `SortedSet` instead of an array * Adding elements to an object literal instead of an array Keep in mind that these alternatives would likely change the performance characteristics of the test cases, and may not be as straightforward to implement. **Benchmark preparation code:** Since there is no script preparation code provided, I assume that the benchmark is simply a matter of executing the JavaScript code snippets. The `while` loop and nested `for` loop create a large array or set of elements, which are then used to test the performance of each approach. **Latest benchmark result:** The latest benchmark results show that the Set Unique approach (adding elements to an empty set) is faster than the Array Unique approach (using `Array.indexOf()`). This makes sense given the time complexity of each method.
Related benchmarks:
Unique array (indexOf) vs Set add
Create an array with unique values - Javascript Array.reduce/Array.indexOf vs Lodash Uniq vs custom fn vs custom2
Filter vs Set (get unique elements)
lodash@4.17.21 uniq vs set vs custom unique
Comments
Confirm delete:
Do you really want to delete benchmark?