Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array.from vs construct manually
(version: 0)
Comparing performance of:
Array.from vs manual
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;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Array.from
manual
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 benchmark and explain what's being tested. **Benchmark Definition** The benchmark is comparing two approaches to create an array from scratch: 1. `Array.from`: This method takes an iterable object (like an array or a string) and creates a new array with all its elements. 2. Manual approach: Instead of using `Array.from`, the code creates an empty array (`z.z = {}`) and then iterates over the input data to populate it. **Options being compared** The two options being compared are: * `Array.from`: A built-in method that creates a new array from an iterable object. * Manual approach: Creating an array manually by iterating over the input data. **Pros and Cons of each approach** **`Array.from`** Pros: * Concise and readable code * Less error-prone, as it handles edge cases for you Cons: * May incur overhead due to method invocation and object lookup * Might be slower than a custom implementation due to the overhead of the `for...of` loop (more on this later) **Manual approach** Pros: * Can be optimized for performance, especially when working with large datasets * Allows direct control over memory allocation and iteration Cons: * More verbose and error-prone code * Requires manual management of array indices and edge cases **Other considerations** In modern JavaScript, using `Array.from` is generally a good choice, as it's concise and readable. However, when working with very large datasets or performance-critical code, a custom implementation might be necessary to squeeze out every last bit of optimization. **Library usage** There is no library being used in this benchmark. **Special JavaScript features/syntax** The `Symbol.iterator` syntax is used to access the iterator protocol for the input data (`src`). This allows us to iterate over the array-like object without explicitly creating an array. However, it's worth noting that the `for...of` loop (used in the manual approach) is also a special feature. It was introduced in ECMAScript 2015 and provides a concise way to iterate over arrays and other iterable objects. While not strictly necessary for this benchmark, it adds a layer of complexity and potential performance overhead compared to using traditional `for` loops. **Alternative implementations** Other alternatives to `Array.from` might include: * Using the spread operator (`[...src]`) to create an array from an array-like object * Utilizing `Array.create()` method to create an empty array * Implementing a custom array creation function using `Buffer.alloc()`, `Uint8Array`, or other low-level data structures Keep in mind that these alternatives might have different performance characteristics and trade-offs compared to the manual approach. Overall, this benchmark provides a useful comparison between two common approaches to creating arrays from scratch, highlighting both the benefits and drawbacks of each method.
Related benchmarks:
create object vs array
Object.fromEntries vs create temp object
Creation of new Array: Array.from vs. new Array
array[0] vs array.at(0)
Map convert
Comments
Confirm delete:
Do you really want to delete benchmark?