Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
fastest way to convert set to array
(version: 0)
Comparing performance of:
spread operator vs cached array.from vs uncached array.from
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var set = new Set([1,41,23,12321,-3, .13, () => {}, {}, []]); var arrFrom = Array.from;
Tests:
spread operator
[...set];
cached array.from
arrFrom(set);
uncached array.from
Array.from(set);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
spread operator
cached array.from
uncached array.from
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/126.0.0.0 Safari/537.36
Browser/OS:
Chrome 126 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
spread operator
6533459.0 Ops/sec
cached array.from
770969.0 Ops/sec
uncached array.from
2731311.8 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and explain what's being tested. **Benchmark Definition** The benchmark definition is a JSON object that provides information about the test case: ```json { "Name": "fastest way to convert set to array", "Description": null, "Script Preparation Code": "var set = new Set([1,41,23,12321,-3, .13, () => {}, {}, []]);\r\nvar arrFrom = Array.from;", "Html Preparation Code": null } ``` Here's what's happening: * A `Set` object is created with some elements. * The `Array.from()` method is assigned to the variable `arrFrom`. * This sets up the test case by preparing the input data and defining the function to convert the Set to an array. **Individual Test Cases** There are three individual test cases: ```json [ { "Benchmark Definition": "[...set];", "Test Name": "spread operator" }, { "Benchmark Definition": "arrFrom(set);", "Test Name": "cached array.from" }, { "Benchmark Definition": "Array.from(set);", "Test Name": "uncached array.from" } ] ``` Each test case defines a different way to convert the Set to an array: 1. `spread operator`: This uses the spread operator (`[...set]`) to create a new array from the Set. 2. `cached array.from`: This uses the cached version of `Array.from()` (since it was previously assigned to the `arrFrom` variable). 3. `uncached array.from`: This uses the default, uncached version of `Array.from()`. **Options Compared** The benchmark is comparing three different approaches: 1. Spread operator (`[...set]`) 2. Cached `array.from()` 3. Uncached `array.from()` Each approach has its pros and cons: * **Spread Operator**: Pros: + Simple to use + Works for any Set. Cons: + May have performance overhead due to the spread operator creation. * **Cached array.from()**: Pros: + Can be faster since the result is cached. Cons: + Requires an initial assignment of `array.from()` before using it, which may incur some overhead. * **Uncached array.from()**: Pros: + Simple and straightforward. Cons: + May have performance overhead due to repeated function calls. **Other Considerations** The benchmark is likely running on a variety of browsers and platforms, so the results may vary depending on the specific environment. Additionally, the use of `new Set()` creates an immutable Set object, which can affect performance. **Libraries Used** None are explicitly mentioned in this benchmark definition. **Special JavaScript Features/Syntax** The spread operator (`[...set]`) is a new feature introduced in ECMAScript 2015 (ES6). It allows creating a new array from an iterable source. This feature was not available in earlier versions of JavaScript and may require specific browser support. That's a summary of the benchmark!
Related benchmarks:
Array clone
fromArray or desctucturing to convert Set to array
Array.from vs. ... expansion
shallow copy of 6M elements array
Comments
Confirm delete:
Do you really want to delete benchmark?