Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Unique values of array
(version: 0)
Comparing performance of:
using Set vs using filter
Created:
6 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var testArray = Array.from(Array(100000)).map(i => Math.floor(Math.random() * 10))
Tests:
using Set
Array.from(new Set(testArray))
using filter
testArray.filter((v, i, a) => a.indexOf(v) === i)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
using Set
using filter
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:137.0) Gecko/20100101 Firefox/137.0
Browser/OS:
Firefox 137 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
using Set
588.4 Ops/sec
using filter
341.1 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and explain what's being tested, compared, and analyzed. **What is tested?** The provided JSON represents two test cases for measuring JavaScript performance. The tests are designed to measure the time taken by different approaches to remove duplicate values from an array of 100,000 random integers. **Options compared:** There are two options compared: 1. **Using a Set**: This approach creates a Set object from the original array. Sets in JavaScript are data structures that store unique values. When you create a Set from an array, it automatically removes any duplicate values. 2. **Using filter()**: This approach uses the `filter()` method to create a new array with only the first occurrence of each value. **Pros and Cons:** 1. **Using a Set:** * Pros: + Most efficient in terms of time complexity (O(n) on average). + Often the recommended way to remove duplicates in JavaScript. * Cons: + Not as straightforward to implement, especially for those unfamiliar with Sets. + May not be suitable for cases where you need to preserve the original order of elements. 2. **Using filter():** * Pros: + Easier to understand and implement for developers who are familiar with `filter()`. + Preserves the original order of elements (if necessary). * Cons: + Has a higher time complexity (O(n^2) in the worst case, although on average it's still relatively efficient). **Other considerations:** When comparing these two approaches, it's essential to consider the following: * **Performance**: For large datasets, using a Set is likely to be faster. * **Code readability and maintainability**: If you're working with a team or need to understand the codebase, using `filter()` might be more intuitive. * **Preserving order**: If preserving the original order of elements is crucial, using `filter()` might be a better choice. **Library usage:** There are no external libraries used in these tests. The Set data structure and the Array.prototype methods (like filter()) are built-in JavaScript features. **Special JS feature or syntax:** No special JavaScript features or syntax are used in these tests. Now, let's look at some alternative approaches: * **Using reduce()**: Instead of using `Set` or `filter()`, you could use the `reduce()` method to remove duplicates. This approach has a similar time complexity to using `Set`. * **Using map() and Array.from()**: You could also use `map()` to create an array with only unique elements, like this: `[...new Set(testArray.map(i => i))]`. However, this approach would be less efficient than using `Set` or `reduce()`, as it involves two extra iterations over the data. * **Manual looping**: You could also use a manual loop to iterate over the array and remove duplicates. This approach would likely be the slowest and most error-prone. Keep in mind that these alternative approaches might not provide the same level of performance or readability as using `Set` or `filter()`.
Related benchmarks:
Methods to remove duplicates from array (fork)
Methods to remove duplicates from array (test)
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)
Comments
Confirm delete:
Do you really want to delete benchmark?