Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Deduplicate 2
(version: 0)
Fixed typo
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[i]) { 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:
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):
I'll break down the benchmark and its options, pros, and cons. **Benchmark Overview** The benchmark tests two JavaScript algorithms: 1. **Deduplicate 2**: Sorts an array of 100,000 elements, removes duplicates, and logs the resulting array to the console. 2. **Set**: Creates a new Set from an array of 100,000 elements (all ones) and logs it. **Options Compared** The benchmark compares two approaches: 1. **Sort Algorithm**: Sorts the array using the built-in `sort()` method and then removes duplicates by comparing adjacent elements in the sorted array. 2. **Set Data Structure**: Creates a new Set from the array, which automatically removes duplicates since sets only store unique values. **Pros and Cons of Each Approach** 1. **Sort Algorithm**: * Pros: + Simple to implement (just two lines of code). + Works on any JavaScript engine. * Cons: + Has a time complexity of O(n log n) due to the `sort()` method, which can be slow for large datasets. + Requires extra memory to store the temporary sorted array. 2. **Set Data Structure**: * Pros: + Has a time complexity of O(n), making it faster than the sort algorithm for large datasets. + Uses less memory since sets only store unique values. * Cons: + Not all JavaScript engines support Set data structures (only modern browsers and some Node.js versions). + May not work in older or non-standard JavaScript environments. **Library Used** In the benchmark, the `Set` function is used from the built-in JavaScript language. The `Set` data structure was introduced in ECMAScript 2015 (ES6) as part of the JavaScript standard. **Special JS Features/Syntax** None mentioned in this specific benchmark, but it's worth noting that other benchmarks might use features like async/await, Promises, or modern syntax like destructuring and arrow functions.
Related benchmarks:
Unnecessary non-capturing groups
Unnecessary non-capturing groups
Unnecessary non-capturing groups
split-join vs regex replace - large input
string.replace with regex: presaved const vs inline
Comments
Confirm delete:
Do you really want to delete benchmark?