Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Set vs Filter vs forEach for unique
(version: 0)
Comparing performance of:
Set spread vs Array from set vs Filter vs Basic forEach
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var array = Array.from({length: 40}, () => Math.floor(Math.random() * 140));
Tests:
Set spread
const f = [... new Set(array)]
Array from set
const s = new Set(array) const l = Array.from(s)
Filter
const b = array.filter((i,index) => array.indexOf(i)=== index)
Basic forEach
const set = {} array.forEach(val => set.hasOwnProperty(val) ? set[val] = true : '') const b = Object.keys(set)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Set spread
Array from set
Filter
Basic forEach
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/131.0.0.0 Safari/537.36 Edg/131.0.0.0
Browser/OS:
Chrome 131 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Set spread
1312297.8 Ops/sec
Array from set
1220334.4 Ops/sec
Filter
259984.4 Ops/sec
Basic forEach
3053994.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the provided benchmark and explain what's being tested, compared, and the pros and cons of each approach. **Benchmark Overview** The benchmark compares four approaches to remove duplicates from an array: 1. `Set` using the spread operator (`Set spread`) 2. Using a `Set` explicitly (`Array from set`) 3. Filtering the original array (`Filter`) 4. Using `forEach` with a boolean flag to track unique elements (`Basic forEach`) **Test Case Explanation** Each test case is defined in a separate object, which includes: * `Benchmark Definition`: The actual code that performs the benchmarking operation. * `Test Name`: A descriptive name for the test case. Here's a brief explanation of each test case: 1. **Set spread**: Creates a new Set from the input array using the spread operator (`[...new Set(array)]`). This method is concise and efficient, but may incur additional overhead due to the creation of an intermediate Set object. 2. **Array from set**: First creates a Set from the input array using `new Set(array)`, then converts it back to an array using `Array.from(s)`. This approach involves two separate operations: creating a Set and converting it to an array. 3. **Filter**: Uses the `filter()` method to create a new array with only unique elements, where each element is checked against its original index in the input array (`array.filter((i, index) => array.indexOf(i) === index)`). This approach has higher overhead due to the need for indexing and comparing elements. 4. **Basic forEach**: Uses `forEach()` with a boolean flag to track unique elements by checking if an element is already present in an object using the `hasOwnProperty` method (`array.forEach((val) => set.hasOwnProperty(val) ? set[val] = true : '')`). This approach also has higher overhead due to the use of `forEach()` and the need for object lookup. **Library Usage** The benchmark uses a few libraries: * None explicitly, but it relies on built-in JavaScript methods like `Set`, `Array.from()`, `filter()`, and `forEach()`. However, some browsers may have slightly different implementations of these methods, which could affect the results. * No external libraries are mentioned in the benchmark definition or test cases. **Special JS Features or Syntax** There are no special JavaScript features or syntax used in this benchmark. The code is straightforward and follows standard JavaScript best practices. **Pros and Cons of Each Approach** Here's a brief summary of the pros and cons of each approach: 1. **Set spread**: Pros: concise, efficient; Cons: may incur additional overhead due to intermediate Set object creation. 2. **Array from set**: Pros: explicit control over data transformation; Cons: two separate operations, potential overhead from Set and array conversion. 3. **Filter**: Pros: simple, easy to understand; Cons: higher overhead due to indexing and comparison, slower performance. 4. **Basic forEach**: Pros: easy to implement, no external dependencies; Cons: higher overhead due to `forEach()` and object lookup. **Other Alternatives** If you're looking for alternative approaches to remove duplicates from an array, consider the following: * Using a hash map (e.g., `Map`) instead of a Set. * Using a more efficient data structure like a trie or suffix tree. * Utilizing specialized libraries like Lodash's `uniq` function. Keep in mind that each approach has its trade-offs and may not be suitable for all use cases. The choice ultimately depends on the specific requirements, performance constraints, and personal preference.
Related benchmarks:
Filter vs Set (get unique elements)
Filter vs Set (unique elements)
Set from array vs array Filter unique
Set vs Good Filter for unique
Comments
Confirm delete:
Do you really want to delete benchmark?