Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Unique Array
(version: 0)
Comparing performance of:
Set vs for loop
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = ['1', '2', '2', '3', '4', '5', '6'];
Tests:
Set
Array.from(new Set(arr))
for loop
const set = []; for (const i of arr) { if (set.indexOf(i) === -1) set.push(i) }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Set
for loop
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):
**Overview of the Benchmark** The provided benchmark, hosted on MeasureThat.net, tests two approaches to creating a unique array from a duplicate array: using the `Array.from(new Set())` method and a simple for loop. **Approach 1: Using `Array.from(new Set())`** This approach creates an instance of `Set`, which automatically removes duplicates from the input array. The `Array.from()` method is then used to convert the set back into an array. **Pros:** * Efficient, as sets are optimized for fast lookup and insertion operations. * Simple to implement in code. * No need to manually iterate through the array or use conditional statements. **Cons:** * May not perform well with large arrays due to the overhead of creating a set. * Requires modern JavaScript versions (ECMAScript 2015+). **Approach 2: Using a for loop** This approach manually iterates through the input array, checks if each element is already present in the resulting array using `indexOf()`, and adds it if not. **Pros:** * Can be more efficient with large arrays, as it avoids creating an intermediate set. * Works with older JavaScript versions (pre-ECMAScript 2015). * No additional libraries or dependencies are required. **Cons:** * More complex to implement in code due to the need for manual iteration and conditional statements. * May have performance issues if the loop iterates slowly or if the `indexOf()` method is slow. **Library Used:** The `Set` object is a built-in JavaScript library, which is available since ECMAScript 2015. It's used in the first approach to remove duplicates from the array. **Special JS Feature/Syntax:** None of the approaches rely on special JavaScript features or syntax beyond what's mentioned above (ECMAScript 2015+). **Other Alternatives:** If you prefer a more concise approach, you could use `Array.from()` with an arrow function to achieve the same result: ```javascript const uniqueArr = Array.from(arr, i => arr.indexOf(i) === -1 ? i : null).filter(Boolean); ``` Keep in mind that this approach still uses modern JavaScript features and may not be suitable for older versions. If you want to use a library or utility function to remove duplicates, you could consider using `lodash` (a popular utility library) or a custom implementation.
Related benchmarks:
Array clone
array.splice vs array.length
arr.slice() vs [...arr]
Clone Array - 08/02/2024
array.length = 0 vs []
Comments
Confirm delete:
Do you really want to delete benchmark?