Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Set vs Array for unique list2
(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 (!b.includes(x)) b.push(x); });
Set
var c = new Set(); a.forEach(x => c.add(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:
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):
Let's break down what's being tested in the provided JSON. **Benchmark Definition** The benchmark is testing two different approaches to create an array of unique values: using a JavaScript array (`Array`) and using a Set data structure. **Options compared** Two options are compared: 1. **Array**: Using a JavaScript array to create a collection of unique values by iterating through another array and adding elements to the first array only if they don't already exist. 2. **Set**: Using a Set data structure to create an unordered collection of unique values by adding elements to it. **Pros and Cons** Here are some pros and cons of each approach: **Array** Pros: * Widely supported in older browsers * Can be used for other purposes beyond storing unique values Cons: * Has a higher memory overhead due to the need to store all elements * May have performance issues if dealing with very large arrays **Set** Pros: * More efficient in terms of memory usage, especially for large datasets * Faster lookup times since sets use hash-based data structures Cons: * May not be supported in older browsers or require additional setup * Limited functionality beyond storing unique values **Library and purpose** In the provided benchmark, the Set library is used, which is a built-in JavaScript object that provides a fast way to store unique values. The purpose of using a Set here is to take advantage of its efficient lookup times. **Special JS feature or syntax** The `forEach` method is used in both test cases, but not specifically mentioned as requiring special handling. **Other alternatives** Some alternative approaches for creating unique arrays could include: * Using the `Reduce` method with an initial value and a callback function * Using the `Filter` method followed by the `Spread` operator or other methods to create a new array However, these alternatives may not offer the same efficiency as using a Set data structure. **Benchmark preparation code** The script preparation code creates an array of 1000 elements filled with null values and then maps each element to its index (b) using the `map` method. This is done to create an array that can be iterated through in the benchmark test cases. The HTML preparation code is empty, indicating that no specific HTML setup is required for this benchmark.
Related benchmarks:
fill array with value: map(callback) vs fill(value)
Map vs preallocation vs slice vs spread
Array.from() vs new Array() performance...
Array.from() vs new Array() - map
fill array with value: map(callback) vs fill(value) 2
Comments
Confirm delete:
Do you really want to delete benchmark?