Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Set vs Array for unique list (convert set to array)2
(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); }); 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:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36
Browser/OS:
Chrome 130 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Unique Array
12203.3 Ops/sec
Set
60140.9 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks. **Benchmark Definition** The provided JSON defines two benchmark tests: 1. **Set vs Array for unique list (convert set to array)2** This test compares the performance of using an array and a set data structure to create a unique list from a large dataset. 2. The second test, "Unique Array" and "Set", are individual microbenchmarks that measure the performance of adding elements to an array and converting it to a unique list using `Array.includes()`, and the performance of using a set to achieve the same result. **Options Compared** The two main options compared in these benchmarks are: 1. **Array**: A traditional JavaScript array, which can be used to store collections of values. 2. **Set**: A data structure that stores unique values, allowing for efficient addition, lookup, and removal of elements. **Pros and Cons** Here's a brief summary of the pros and cons of each approach: * **Array**: + Pros: Easy to use, flexible, and widely supported. + Cons: Can be slow for large datasets due to the need to iterate through all elements to check for uniqueness. * **Set**: + Pros: Efficient insertion, lookup, and removal of unique values, making it suitable for large datasets. + Cons: May not be as intuitive or easy to use for small datasets. **Library/Functionality Used** In the provided benchmark tests, no specific libraries are used. However, the `Array.from()` function is employed in one of the test cases to convert a set to an array. This function returns a new array from an iterable or an array-like object, which is useful for converting sets to arrays. **Special JS Feature/Syntax** The benchmark tests use a JavaScript feature called "arrow functions" (e.g., `x => b`) to define small, anonymous functions. Arrow functions are a shorthand way of defining functions that were previously defined using the `function` keyword. They provide a concise and readable syntax for simple function definitions. **Other Considerations** When designing microbenchmarks like these, it's essential to consider factors such as: * **Dataset size**: The size of the dataset being tested can significantly impact performance. * **Browser support**: Ensure that the benchmark tests work on multiple browsers, including older versions. * **Platform**: Test the benchmarks on various devices and platforms to ensure consistency. **Alternatives** Other alternatives for creating unique lists include using a `Map` data structure or a library like Lodash's `uniqBy()` function. However, these approaches may introduce additional overhead or complexity compared to using an array or set. For example, using a `Map` can be useful if you need to perform additional operations on the elements being added to the map: ```javascript const map = new Map(); a.forEach(x => { if (!map.has(x)) map.set(x, x); }); ``` Similarly, Lodash's `uniqBy()` function provides a convenient way to create unique arrays while ignoring certain properties: ```javascript import _ from 'lodash'; _.uniqBy(a, 'property'); ``` In summary, the provided benchmark tests compare the performance of using an array and a set data structure to create unique lists. The choice of approach depends on the specific requirements and constraints of your application.
Related benchmarks:
Set vs Array for unique list
Set vs Array for unique list (convert set to array)
Set vs Array for unique list2
Set vs Array for unique list (convert set to array) 2
Comments
Confirm delete:
Do you really want to delete benchmark?