Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array.from() vs Spread with 10K elements
(version: 0)
Comparing performance of:
Array.from() vs Spread
Created:
one year ago
by:
Guest
Jump to the latest result
Tests:
Array.from()
let set = new Set(); for (var i = 0; i < 10_000; i++) { set.add(i); } let result = Array.from(set);
Spread
let set = new Set(); for (var i = 0; i < 10_000; i++) { set.add(i); } let result = [...set];
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Array.from()
Spread
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:129.0) Gecko/20100101 Firefox/129.0
Browser/OS:
Firefox 129 on Ubuntu
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Array.from()
1341.5 Ops/sec
Spread
1148.4 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks and analyze what's being tested on the provided JSON. **Benchmark Definition** The benchmark is defined as an array containing two test cases: 1. `Array.from() vs Spread with 10K elements` 2. `let set = new Set(); for (var i = 0; i < 10_000; i++) { set.add(i); } let result = Array.from(set);` and 3. `let set = new Set(); for (var i = 0; i < 10_000; i++) { set.add(i); } let result = [...set];` These test cases aim to compare the performance of two approaches: using `Array.from()` with a new `Set` object, and using spread syntax (`...`) with a new `Set` object. **Options Compared** The two options being compared are: 1. **Using `Array.from()`**: This method creates a new array from an iterable (in this case, the `Set` object). It's a convenient way to convert sets to arrays. 2. **Using spread syntax (`...`) with a new `Set` object**: This approach creates a new array by spreading the elements of the `Set` object. **Pros and Cons** * **Using `Array.from()`**: + Pros: More concise, readable code. It's also more efficient because it avoids creating an intermediate array. + Cons: May have performance overhead due to the creation of an intermediate array. * **Using spread syntax (`...`) with a new `Set` object**: + Pros: Avoids creating an intermediate array, which can lead to better performance. + Cons: May be less readable and more verbose than using `Array.from()`. **Library Used** None are explicitly mentioned in the benchmark definition. However, both approaches use built-in JavaScript features (`Set`, `Array.from()`, spread syntax). **Special JS Feature/Syntax** The test case uses a feature introduced in ECMAScript 2015 (ES6): the `let` and `var` declarations with block scope. In older versions of JavaScript (e.g., ES5), the `var` keyword would have used function scope, which could lead to unexpected behavior. The use of `let` instead of `var` helps avoid this issue by introducing a new scope for each block. **Other Considerations** When writing performance benchmarks, it's essential to consider factors like: * Input size: In this case, the benchmark is testing with 10,000 elements. * System configuration: The results may vary depending on the system configuration (e.g., CPU architecture, operating system). * Cache effects: The order of operations and caching behavior can impact performance. **Alternatives** Other alternatives to test these scenarios could include: 1. Using `Array.from()` with an existing array instead of a `Set`. 2. Using other spread syntax variants (e.g., using `Array.prototype.slice()` or `Array.prototype.splice()`). 3. Comparing the performance of different JavaScript engines or browsers. In conclusion, this benchmark helps compare the performance of two approaches for converting sets to arrays: using `Array.from()` and using spread syntax with a new `Set` object. The test results provide insights into which approach is more efficient in terms of execution rate.
Related benchmarks:
Array.prototype.slice vs spread op
Spread vs Slice operators in JS
Array.prototype.slice vs spread operator - large array 100000
Array.from vs Spread using 1000000 elements / only counts conversion
Array.from vs Spread using 10000 elements / only counts conversion
Comments
Confirm delete:
Do you really want to delete benchmark?