Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Set vs Array for unique list (convert set to array)
(version: 0)
Comparing performance of:
Unique Array vs Set
Created:
5 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var a = Array(1000).fill(null).map((a, b) => b);
Tests:
Unique Array
var b = []; a.forEach(x => { if (!a.includes(x)) a.push(x); }); a.map(x => x);
Set
var c = new Set(); a.forEach(x => c.add(x)); var b = Array.from(a); b.map(x => x);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Unique Array
Set
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36
Browser/OS:
Chrome 120 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Unique Array
8370.8 Ops/sec
Set
20433.6 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'd be happy to explain the benchmark being tested on MeasureThat.net. **Benchmark Definition** The benchmark is defined by two test cases: 1. **"Unique Array"`**: This test case creates an array `a` with 1000 null values and then uses the `forEach` method to iterate over each value in the array. Inside the callback function, it checks if the current value `x` is not already included in the array using the `includes` method. If it's not included, it pushes `x` onto the end of the array. Finally, it maps over the array again to produce an empty array. 2. **"Set"`**: This test case creates a set `c` and then uses the `forEach` method to iterate over each value in the array `a`. Inside the callback function, it adds each value `x` to the set using the `add` method. After adding all values to the set, it converts the set to an array `b` using `Array.from()` and maps over the array again to produce an empty array. **Options Compared** The two test cases are comparing the performance of different approaches: * Using a JavaScript array (`"Unique Array"`): This approach uses the `includes` method inside the callback function, which may lead to slower performance due to the potential for linear search. * Using a JavaScript set (`"Set"`): This approach uses the `add` method and then converts the set to an array using `Array.from()`. Sets are designed for fast membership testing and efficient insertion of elements. **Pros and Cons** * **Using a JavaScript array (Unique Array)** + Pros: - Simpler implementation + Cons: - May be slower due to linear search in the includes method - More memory-intensive due to the need to store all elements * **Using a JavaScript set (Set)** + Pros: - Faster membership testing and efficient insertion of elements - More memory-efficient due to only storing unique values + Cons: - More complex implementation - May require additional operations like converting the set to an array **Library** In both test cases, no external libraries are used. **Special JS Features/Syntax** None of these test cases use any special JavaScript features or syntax. They only rely on standard JavaScript methods and data structures (arrays and sets). **Other Alternatives** If you want to optimize the unique array approach: * Use a `Map` instead of an array, as it allows for fast lookups using the `has` method. * Pre-create the array with all values already present, then use the `includes` method or a custom loop to find missing values. However, these optimizations might not be worth the additional complexity if performance differences are negligible.
Related benchmarks:
Set vs Array for unique list
Set vs Array for unique list2
Set vs Array for unique list (convert set to array)2
Set vs Array for unique list (convert set to array) 2
set vs array iteration new new
Comments
Confirm delete:
Do you really want to delete benchmark?