Toggle navigation
MeasureThat
.net
Create a benchmark
Tools
Feedback
Feature Requests
FAQ
Register
Log In
Get array from iterable with Array.from() vs for loop
(version: 0)
Benchmark.js
Array.from is notoriously slow. This compares creating an Array from an iterable with Array.from() vs a for loop.
Comparing performance of:
Array.from() vs for loop
Created:
3 years ago
by:
Guest
Go to the latest result
Tests:
Array.from()
var iterable = document.all,a = Array.from(iterable)
for loop
for(var iterable=document.all,a=Array(iterable.length),iterator=iterable[Symbol.iterator](),i=0,next;!(next=iterator.next()).done;)a[i++]=next.value;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Array.from()
for loop
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
10 days ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/152.0.0.0 Safari/537.36
Browser/OS:
Chrome 152 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
for loop
650K/s
Array.from()
536K/s
View exact numbers
Test name
Executions per second
✓
for loop
649,522 Ops/sec
Array.from()
535,656 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark. **Benchmark Definition:** The benchmark compares two approaches for creating an array from an iterable: 1. `Array.from()` 2. For loop (using the iterator protocol) **What is being tested?** In this case, we're testing which approach is faster and more efficient in terms of execution speed on a specific JavaScript engine. **Options compared:** * `Array.from()` vs for loop **Pros and Cons:** 1. `Array.from()`: This method is often considered convenient and easy to read, as it uses the spread operator (`...`) to create an array from an iterable. However, performance-wise, this approach can be slower than a traditional for loop. 2. For loop (iterator protocol): This approach is generally faster and more efficient because it allows the engine to optimize the iteration process using the iterator protocol. **Other considerations:** * Performance: The goal of this benchmark is to determine which approach yields better performance on the specified JavaScript engine. * Code readability and maintainability: While `Array.from()` might be more concise, a for loop can provide better insight into the underlying iteration mechanism. * Platform dependence: The results may vary depending on the platform (e.g., browser, Node.js) and JavaScript engine. **Library and syntax considerations:** The benchmark uses the `document.all` property to create an iterable from the DOM. This suggests that the test is running in a web environment. No special JavaScript features or syntax are being used in this benchmark. **Alternatives:** If you're interested in exploring alternative approaches, here are a few options: * Using other array creation methods like `Array.from()` with a custom function to create an iterable (e.g., using `map()`, `reduce()`, or `slice()`) versus a for loop. * Comparing performance with different browsers or JavaScript engines (e.g., Chrome, Firefox, Node.js). * Adding more test cases that cover different scenarios, such as: + Creating arrays from objects or other data structures. + Handling large datasets or edge cases. Keep in mind that these alternatives may require significant modifications to the benchmark code.
Related benchmarks
Array.from() vs new Array()
Array.from({ length: n }) vs new Array(n)
Array.from() vs new Array() with index
Comments
Confirm delete:
Do you really want to delete benchmark?