Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
JS Loops tst
(version: 0)
Comparing performance of:
Array from vs Fill vs for loop vs while loop
Created:
3 years ago
by:
Guest
Jump to the latest result
Tests:
Array from
const arr = Array.from({ length: 10 }, () => []);
Fill
const arr2 = Array(10) .fill() .map(() => []);
for loop
const arr3 = []; for (let i = 0; i < 10; i += 1) { arr3.push([]); }
while loop
const arr4 = []; let i = 0; while (i < 10) { arr4.push([]); i++; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Array from
Fill
for loop
while loop
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. **What is being tested?** The benchmark tests the performance of three different ways to create an array of empty arrays in JavaScript: 1. **Array.from()**: This method creates a new array from an iterable or an array-like object. 2. **Array.fill() + map()**: This approach uses the `fill()` method to fill the entire array with a specified value, and then maps over the array to create a new array of empty arrays. 3. **for loop**: A traditional for loop that pushes empty arrays onto the result array. 4. **while loop**: Similar to the for loop, but uses a while loop instead. **Options being compared** The benchmark is comparing the performance of these four approaches: * Array.from() * Array.fill() + map() * for loop * while loop **Pros and Cons** Here's a brief summary of the pros and cons of each approach: 1. **Array.from()**: Pros: * Concise and expressive syntax. * Fast, as it uses optimized C++ code under the hood. * Cons: + May not be supported in older browsers or Node.js versions. 2. **Array.fill() + map()**: Pros: * Can be used to fill an array with a specific value, making it useful for other tasks. * Fast, as it uses optimized C++ code under the hood. * Cons: + Requires two separate operations (fill and map), which may incur overhead. 3. **for loop**: Pros: * Easy to understand and implement for those familiar with traditional loops. * No additional libraries or modules needed. * Cons: + May be slower than the other approaches, as it involves pushing elements onto an array using a loop. 4. **while loop**: Pros: * Similar performance to the for loop, but may be slightly faster due to fewer overheads. * Easy to understand and implement for those familiar with traditional loops. **Library usage** None of the benchmark cases explicitly use any external libraries or modules besides the built-in JavaScript Array prototype. **Special JS features or syntax** There are no special JavaScript features or syntax being tested in this benchmark. The focus is solely on the performance comparison of different array creation approaches. **Other alternatives** If you'd like to create an array of empty arrays, there's also a fifth approach: 5. **Array constructor**: You can use the Array constructor to create a new array and push empty arrays onto it: ```javascript const arr = []; for (let i = 0; i < 10; i++) { arr.push([]); } ``` This approach is similar to the for loop, but uses the Array constructor instead. Keep in mind that this benchmark is primarily focused on comparing performance between different array creation approaches. If you're looking to optimize your own code or explore alternative solutions, I recommend checking out MeasureThat.net's documentation and resources for more information!
Related benchmarks:
Looping
loop compary
Loop Test (forEach vs for)
For loop test #2
Array looping - for, Array.forEach, for-of, array.entries()
Comments
Confirm delete:
Do you really want to delete benchmark?