Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array.from(new Set(array)) vs [...new Set(array)]
(version: 0)
Comparing performance of:
Array.from(new Set(array)) vs [...new Set(array)]
Created:
3 years ago
by:
Guest
Jump to the latest result
Tests:
Array.from(new Set(array))
const numbers = [2,3,4,4,2,3,3,4,4,5,5,6,6,7,5,32,3,4,5,2,3,4,4,2,3,3,4,4,5,5,6,6,7,5,32,3,4,5,2,3,4,4,2,3,3,4,4,5,5,6,6,7,5,32,3,4,5,2,3,4,4,2,3,3,4,4,5,5,6,6,7,5,32,3,4,5,2,3,4,4,2,3,3,4,4,5,5,6,6,7,5,32,3,4,5,2,3,4,4,2,3,3,4,4,5,5,6,6,7,5,32,3,4,5,2,3,4,4,2,3,3,4,4,5,5,6,6,7,5,32,3,4,5,2,3,4,4,2,3,3,4,4,5,5,6,6,7,5,32,3,4,5,2,3,4,4,2,3,3,4,4,5,5,6,6,7,5,32,3,4,5,2,3,4,4,2,3,3,4,4,5,5,6,6,7,5,32,3,4,5,2,3,4,4,2,3,3,4,4,5,5,6,6,7,5,32,3,4,5,2,3,4,4,2,3,3,4,4,5,5,6,6,7,5,32,3,4,5,2,3,4,4,2,3,3,4,4,5,5,6,6,7,5,32,3,4,5,2,3,4,4,2,3,3,4,4,5,5,6,6,7,5,32,3,4,5,2,3,4,4,2,3,3,4,4,5,5,6,6,7,5,32,3,4,5]; const arr = Array.from(new Set(numbers)); console.log(arr);
[...new Set(array)]
const numbers = [2,3,4,4,2,3,3,4,4,5,5,6,6,7,5,32,3,4,5,2,3,4,4,2,3,3,4,4,5,5,6,6,7,5,32,3,4,5,2,3,4,4,2,3,3,4,4,5,5,6,6,7,5,32,3,4,5,2,3,4,4,2,3,3,4,4,5,5,6,6,7,5,32,3,4,5,2,3,4,4,2,3,3,4,4,5,5,6,6,7,5,32,3,4,5,2,3,4,4,2,3,3,4,4,5,5,6,6,7,5,32,3,4,5,2,3,4,4,2,3,3,4,4,5,5,6,6,7,5,32,3,4,5,2,3,4,4,2,3,3,4,4,5,5,6,6,7,5,32,3,4,5,2,3,4,4,2,3,3,4,4,5,5,6,6,7,5,32,3,4,5,2,3,4,4,2,3,3,4,4,5,5,6,6,7,5,32,3,4,5,2,3,4,4,2,3,3,4,4,5,5,6,6,7,5,32,3,4,5,2,3,4,4,2,3,3,4,4,5,5,6,6,7,5,32,3,4,5,2,3,4,4,2,3,3,4,4,5,5,6,6,7,5,32,3,4,5,2,3,4,4,2,3,3,4,4,5,5,6,6,7,5,32,3,4,5,2,3,4,4,2,3,3,4,4,5,5,6,6,7,5,32,3,4,5]; const arr = [...new Set(numbers)]; console.log(arr);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Array.from(new Set(array))
[...new Set(array)]
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 its results. **Benchmark Definition:** The benchmark is designed to compare two approaches for converting an array to a set and then back to an array: 1. `Array.from(new Set(array))` 2. `[...new Set(array)]` These two approaches are tested using a large sample array of numbers, and the performance differences between them are measured. **Options Compared:** The benchmark compares two options: * **Option 1:** Using the `Array.from()` method in conjunction with the `Set` constructor to convert an array to a set. * **Option 2:** Using the spread operator (`...`) along with the `Set` constructor to create a new set from an array. **Pros and Cons of Each Approach:** 1. **Option 1 (Array.from() + Set)**: * Pros: More explicit and readable code, as it clearly conveys the intent of converting an array to a set. * Cons: May have performance overhead due to creating an intermediate `Set` object. 2. **Option 2 ([...new Set(array)])**: * Pros: More concise and expressive syntax, as it directly converts an array to a set without creating an intermediate object. * Cons: Less explicit about the intent of the operation, which might lead to misunderstandings. **Other Considerations:** * Both approaches assume that the input array contains unique values. If the array contains duplicates, this could impact performance and accuracy. * The benchmark uses Chrome 103 as the browser and Linux as the operating system. Results may vary on other platforms or browsers. **Library Used:** None of the test cases explicitly use a library, but they do rely on the built-in `Set` constructor and the spread operator (`...`). **Special JS Feature/Syntax:** The benchmark uses the spread operator (`...`) to create a new set from an array. This is a modern JavaScript feature introduced in ECMAScript 2015 (ES6).
Related benchmarks:
Array.from() vs new Array()
my Array.from() vs new Array()
Array.from() vs new Array() - empty
Array.from() vs new A
Array.from() vs new Array() vs []
Comments
Confirm delete:
Do you really want to delete benchmark?