Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array.from vs spread vs construct manually 2
(version: 0)
Comparing performance of:
Array.from vs manual vs spread vs manual 2
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]())];
manual 2
let a = Array(src.length); let iter = src[Symbol.iterator](); for (let i = 0, obj = iter.next(); !obj.done; obj = iter.next()) { a[i++] = obj.value;}; z.z = a;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Array.from
manual
spread
manual 2
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):
**What is being tested?** The provided JSON represents a JavaScript microbenchmark test case, specifically comparing the performance of three approaches for creating an array from a given iterable: 1. `Array.from` 2. Manual iteration using a loop 3. Spread operator (`...`) **Options compared:** * `Array.from`: A built-in function that creates a new array from an iterable. * Manual iteration: Using a loop to iterate over the iterable and push each value into a new array. * Spread operator (`...`): Using the spread operator to create a new array from an iterable. **Pros and Cons of each approach:** * `Array.from`: + Pros: concise, efficient, and easy to use. It's also a built-in function, which means it's optimized for performance. + Cons: may have higher overhead due to the creation of a temporary array. * Manual iteration: + Pros: flexible, can be used with any iterable, and doesn't create a temporary array. + Cons: more verbose, and requires careful handling of edge cases (e.g., iterating over an empty iterable). * Spread operator (`...`): + Pros: concise, easy to use, and efficient. It's also a modern feature that's widely supported in JavaScript engines. + Cons: may not work as expected with certain iterables or browsers, and can lead to unexpected behavior if not used carefully. **Library usage:** None of the test cases explicitly uses any libraries. **Special JS features/syntax:** The test case uses the `Symbol.iterator` syntax, which is a built-in feature in JavaScript that allows you to iterate over an object using a for...of loop or by creating an iterator manually. This syntax is used to create an iterator from the original array of keys (`src`) and then use it to populate the new array (`a`). **Other considerations:** * The test case uses Chrome 69 as the browser, which may have its own quirks and limitations. * The device platform is set to Desktop, which might affect the results due to differences in hardware or software configurations. * The operating system is set to Linux, which may influence the performance due to the underlying kernel or system optimizations. **Alternative approaches:** If you're interested in exploring other options, here are a few alternative approaches: 1. Using `Array.prototype.concat()`: Instead of using spread operator (`...`), you could use `concat()` method to concatenate arrays. 2. Using `Array.from()` with a callback function: You could pass a callback function to `Array.from()` to transform the values in the new array. 3. Using a library like Lodash: If you need more advanced iteration or transformation capabilities, you could consider using a library like Lodash, which provides a range of utility functions for working with arrays and iterables. Keep in mind that these alternative approaches might have different performance characteristics or use cases compared to the original three approaches.
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?