Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array.from vs spread vs construct manually
(version: 0)
Comparing performance of:
Array.from vs manual vs spread
Created:
7 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var src = Object.keys([...Array(10000)]); var z = {};
Tests:
Array.from
z.z = Array.from(src[Symbol.iterator]());
manual
let n = src.length; let a = Array(n); let iter = src[Symbol.iterator](); for (let i = 0; i < n; ++i) {a[i] = iter.next().value;}; z.z = a;
spread
z.z = [...(src[Symbol.iterator]())];
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Array.from
manual
spread
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 dive into explaining the provided JSON benchmark. **Benchmark Purpose:** The purpose of this benchmark is to compare three approaches for creating an array from a large dataset: 1. Using `Array.from()` 2. Spreading an iterator manually 3. Constructing the array manually using a loop These approaches are compared in terms of their performance, measured by the number of executions per second. **Options Compared:** * **`Array.from()`**: A built-in JavaScript method that creates a new array from an iterable (e.g., an iterator) or an array-like object. * **Spreading an iterator manually**: This involves using the spread operator (`...`) to create a new array by iterating over an existing array or iterator. * **Constructing the array manually**: This approach uses a loop to push elements onto a new array. **Pros and Cons:** * **`Array.from()`**: + Pros: concise, readable, and efficient for most use cases. + Cons: may incur overhead due to its built-in nature, potentially slower than manual approaches for very large datasets. * **Spreading an iterator manually**: + Pros: allows for more control over the iteration process, can be faster for large datasets. + Cons: less readable and maintainable than `Array.from()`, may require additional code to handle edge cases. * **Constructing the array manually**: + Pros: provides complete control over the iteration process, can be the fastest approach for very large datasets. + Cons: verbose, error-prone, and less readable. **Library/Functionality:** In this benchmark, the `Symbol.iterator` method is used to create an iterator from the original array. This allows us to iterate over the elements of the array without creating a temporary copy. **Special JavaScript Feature/Syntax:** The use of `Spread operator (`...`) and `Symbol.iterator` are standard features in modern JavaScript. The spread operator is used to create a new array by iterating over an existing array or iterator, while `Symbol.iterator` allows us to define custom iteration protocols for objects. **Other Alternatives:** If you need to create an array from a large dataset, other approaches might include: * Using `Array.prototype.slice()` or `Array.prototype.concat()` * Utilizing libraries like Lodash or Ramda * Employing parallel processing techniques using web workers or other concurrency models Keep in mind that the best approach depends on your specific use case, performance requirements, and personal preference.
Related benchmarks:
Array.from vs Spread 5
Array.from vs Spread Arrays
Javascript string to array mapping: Array.from() vs Spread syntax [...spread]
spread vs ArrayFrom
Array.from vs Spread (1000 numbers)
Comments
Confirm delete:
Do you really want to delete benchmark?