Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Deduplicate
(version: 0)
Comparing performance of:
Sort vs Set
Created:
2 years ago
by:
Guest
Jump to the latest result
Tests:
Sort
let array = []; for (let i = 0; i < 100000; i++) { array.push(1); array.push(i); } array.sort(); let newArray = [array[0]]; for(let i = 0; i < array.length; i ++) { if(array[i-1] !== array[1]) { newArray.push(array[i]) } } console.log(newArray);
Set
let array = []; for (let i = 0; i < 100000; i++) { array.push(1); array.push(i); } console.log([...new Set(array)]);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Sort
Set
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/128.0.0.0 Safari/537.36
Browser/OS:
Chrome 128 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Sort
50.2 Ops/sec
Set
116.7 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into explaining the provided benchmark. **Benchmark Overview** The given benchmark tests two individual test cases, each with a unique approach to solve a problem: deduplicating an array of numbers. The benchmark aims to compare the performance of these two approaches across different browsers and platforms. **Test Cases** There are only two test cases: 1. **Sort**: This approach sorts the entire array first and then creates a new array by pushing elements to it if they don't match the previous element (excluding the first one). The idea is to remove duplicates while preserving the original order of unique elements. 2. **Set**: This approach uses the `Set` data structure, which automatically removes duplicates. It converts the array to an iterable, passes it through a `Set`, and then converts it back to an array. **Options Compared** The benchmark compares two distinct approaches: 1. Sorting the entire array and then filtering out duplicates. 2. Using a `Set` to remove duplicates directly from the original array. **Pros and Cons of Each Approach** 1. **Sort**: Pros: * Easy to implement and understand for those familiar with sorting algorithms. * Can be optimized using various sorting algorithms (e.g., quicksort, mergesort) for better performance. Cons: * Requires additional memory for storing the sorted array. * May perform poorly on large datasets due to its linear time complexity. 2. **Set**: Pros: + Efficient in terms of memory usage and has a lower time complexity (O(n)). + Automatically removes duplicates, reducing code complexity. However, there are some potential downsides: * Not all browsers support `Set` data structures natively. * Depending on the specific use case, it might require additional processing to convert between arrays and sets. **Library Used** In the provided benchmark, the `Set` data structure is used. A `Set` is a collection of unique values, which allows you to add elements without duplicates. **Special JS Features or Syntax** None are explicitly mentioned in the provided benchmark code. However, it's worth noting that this benchmark might be relevant for developers working with JavaScript and its built-in data structures like `Set`. **Other Alternatives** If you need to deduplicate an array, there are a few other approaches: 1. **Using `filter()`**: You can use the `filter()` method in combination with a predicate function to remove duplicates. ```javascript const newArray = array.filter((value, index) => { return array.findIndex((v) => v === value) === index; }); ``` 2. **Using `reduce()` and a set**: Similar to the `Set` approach, you can use `reduce()` to create a new array with unique values. ```javascript const newArray = array.reduce((acc, current) => { if (!acc.has(current)) { acc.add(current); return acc; } return acc; }, new Set()); ``` These alternatives are more concise and might be preferred by some developers. However, the `Set` approach remains a clear winner in terms of performance. The final answer is a mix of both approaches: while sorting can provide a simpler implementation, using a `Set` offers better performance for this specific use case.
Related benchmarks:
cloneMultiple vs cloneNode
JavaScript spread operator vs cloneDeep
Deep clone object vs map
Class cost
Lodash isEqual test (slightly bigger test data)
Comments
Confirm delete:
Do you really want to delete benchmark?