Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array.from vs forEach (mytest)
(version: 0)
Comparing performance of:
Array.from vs forEach
Created:
2 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var fooSet = new Set(); for(var i=0;i<100;i++) { fooSet.add(i); } var other = [];
Tests:
Array.from
other = Array.from(fooSet);
forEach
fooSet.forEach(foo => other.push(foo));
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Array.from
forEach
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
8 months ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64; rv:142.0) Gecko/20100101 Firefox/142.0
Browser/OS:
Firefox 142 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Array.from
830906.4 Ops/sec
forEach
292558.1 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'd be happy to explain the benchmark! The provided JSON represents a JavaScript microbenchmark that compares two approaches for creating an array from a Set object: `Array.from` and the `forEach` method. **What is being tested?** The benchmark tests the performance of two different methods to create an array from a Set object: 1. **Array.from**: This method creates a new array with the elements of the given iterable (in this case, the `fooSet` Set object). 2. **forEach**: This method iterates over each element in the set and adds it to another array (`other`). **Options compared** The benchmark compares two options: 1. **Array.from**: A built-in JavaScript method that creates an array from an iterable. 2. **forEach**: A built-in JavaScript method that executes a callback function for each element in an iterable. **Pros and Cons of each approach:** * **Array.from**: + Pros: - More efficient, as it avoids the overhead of iterating over the set manually using `forEach`. - Less error-prone, as it doesn't require manual iteration. + Cons: - Requires creating an additional array to store the results. - May not be suitable for very large sets, as it can consume more memory. * **forEach**: + Pros: - Lightweight and easy to understand. - Suitable for very large sets, as it doesn't require creating a new array. + Cons: - Less efficient than `Array.from`, as it iterates over the set manually using `push`. - More error-prone, as it requires manual iteration. **Library and purpose** There is no specific library used in this benchmark. The Set object and Array methods are part of the built-in JavaScript API. **Special JS feature or syntax** The benchmark doesn't use any special JavaScript features or syntax that would be unfamiliar to most software engineers. However, if you're not familiar with sets or arrays, it's worth noting that these data structures are a fundamental part of JavaScript programming. **Other alternatives** There are other ways to create an array from a set in JavaScript, such as using the spread operator (`[...fooSet]`) or the `map()` method. However, these approaches may not be comparable to `Array.from` and `forEach` in terms of performance. For example, using the spread operator: ```javascript var other = [...fooSet]; ``` This approach is more concise but may not be as efficient as `Array.from`. Using the `map()` method: ```javascript var other = fooSet.map(x => x); ``` This approach is also less efficient than `Array.from` and may require more memory. Overall, the benchmark provides a useful comparison between two common approaches to creating an array from a set in JavaScript.
Related benchmarks:
Array.from vs Spread #2
Array.from vs Spread, properly prepared
Array.from vs. Spread
Array.from vs Spread (1000 numbers)
Comments
Confirm delete:
Do you really want to delete benchmark?