Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Set vs Array for unique list
(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); });
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 the provided benchmark and explain what's being tested. **Benchmark Overview** The benchmark is designed to compare two approaches for creating unique lists: using an array (`Array`) versus using a `Set`. The goal is to determine which approach is faster, more efficient, or more suitable for certain use cases. **Script Preparation Code** The script preparation code initializes an array `a` with 1000 elements and fills it with null values. This is done using the following JavaScript code: ```javascript var a = Array(1000).fill(null).map((a, b) => b); ``` This code creates an array with 1000 elements, each initialized to null. The `map()` function is used to transform each element in the array (which is initially null) into its index (`b`). **Test Cases** There are two test cases: 1. **Unique Array**: This test case uses the `forEach()` method to iterate over the array `a`. For each element, it checks if the element is not already included in the array using the `includes()` method. If the element is not found, it's added to the array. ```javascript a.forEach(x => { if (!a.includes(x)) a.push(x); }); ``` This approach has a time complexity of O(n^2), making it less efficient for large arrays. 2. **Set**: This test case uses a `Set` object to create a unique list. It iterates over the array `a` and adds each element to the set using the `add()` method. ```javascript var c = new Set(); a.forEach(x => c.add(x)); ``` This approach has a time complexity of O(n), making it more efficient for large arrays. **Benchmark Results** The benchmark results show two browsers, Chrome 104, executing each test case. The results indicate that: * For the `Set` test case, Chrome 104 executes approximately 88,088 iterations per second. * For the `Unique Array` test case, Chrome 104 executes approximately 35,580 iterations per second. **Pros and Cons** Here are some pros and cons of each approach: **Array (Unique Array)** Pros: * More intuitive for developers who are familiar with array manipulation * Can be more flexible for certain use cases (e.g., modifying the array as elements are added) Cons: * Has a higher time complexity (O(n^2)) compared to using a `Set`, making it less efficient for large arrays. **Set** Pros: * More efficient for large arrays due to its O(n) time complexity * Provides faster lookup and insertion times Cons: * May be less intuitive for developers who are not familiar with `Set` objects * Can be slower for smaller arrays or arrays with fewer unique elements. **Other Considerations** When choosing between these approaches, consider the following factors: * Performance: If speed is crucial, using a `Set` might be a better choice. * Readability and maintainability: Using an array (`Unique Array`) might be more suitable if code readability and maintainability are prioritized. * Specific use cases: Depending on the specific requirements of your application, one approach might be more suitable than the other. **Alternative Approaches** Other alternative approaches for creating unique lists include: * `Map`: Similar to `Set`, but can store additional data associated with each key-value pair. * `WeakSet`: A specialized `Set` that only tracks weak references, which can be useful in certain scenarios where memory management is crucial. * Custom implementation using a hash table or other data structure. Keep in mind that these alternative approaches might have different trade-offs and use cases compared to the original `Set` approach.
Related benchmarks:
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?