Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Create New Array & Fill it
(version: 0)
Comparing performance of:
Map vs Array.apply + Fill vs While Loop vs For Loop vs New Array + Fill vs Array Literal With Length + Fill
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var length = 1000000; var fillWith = 5;
Tests:
Map
const array = [...Array(length)].map(() => fillWith);
Array.apply + Fill
const array = Array.apply([], { length }).fill(fillWith);
While Loop
let index = length - 1; const array = []; while (index-- > 0) array[index] = fillWith;
For Loop
const array = []; for (let index = length - 1; index >= 0; index--) array[index] = fillWith;
New Array + Fill
const array = new Array(length).fill(fillWith);
Array Literal With Length + Fill
const array = []; array.length = length; array.fill(fillWith);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (6)
Previous results
Fork
Test case name
Result
Map
Array.apply + Fill
While Loop
For Loop
New Array + Fill
Array Literal With Length + Fill
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 JavaScript microbenchmark and explain what's being tested. **Benchmark Purpose:** The benchmark measures the performance of different ways to create and fill an array in JavaScript. **Options Compared:** 1. `Array.prototype.map()` 2. `Array.apply()` with a custom length and fill value 3. While loop 4. For loop 5. `new Array(length).fill(fillValue)` (a modern way of creating arrays) 6. `[]` literal with `length` property set to the desired size, followed by `fill()` method **Pros and Cons:** 1. **`Array.prototype.map()`**: Pros - concise and readable; Cons - may incur additional overhead due to iterating over the array. 2. **`Array.apply()` with a custom length and fill value**: Pros - allows for fine-grained control over the array's creation; Cons - can be less readable and may have performance implications due to the use of `apply()`. 3. **While loop**: Pros - can be more efficient in some cases since it avoids creating an intermediate array; Cons - may require additional variables and logic. 4. **For loop**: Pros - similar to while loops, but with a more traditional approach; Cons - also requires additional variables and logic. 5. **`new Array(length).fill(fillValue)`**: Pros - modern, concise, and efficient; Cons - not supported in older browsers. 6. **`[]` literal with `length` property set to the desired size, followed by `fill()` method**: Pros - concise and readable; Cons - may have performance implications due to the use of `fill()` **Library Usage:** None of the options directly use a JavaScript library. **Special JS Features/Syntax:** The benchmark uses modern features like `const` (const declaration), template literals (`\r\n` in the script preparation code is not used, but this example shows its usage), and array destructuring (`Array(length)`). These features are part of ECMAScript 2015 (ES6) syntax. **Alternatives:** Other ways to create arrays could be tested, such as: * Using `Math.pow()` or other numerical methods to calculate the desired size * Using a library like Lodash or Underscore.js for array creation and manipulation * Using a different approach altogether, like using an array buffer or a typed array However, these alternatives may not provide meaningful results or are already covered by existing benchmarks. Overall, this benchmark provides a comprehensive comparison of various methods for creating and filling arrays in JavaScript, covering both modern and traditional approaches.
Related benchmarks:
Zero-fill Uint8Array
fill vs map
fill-fast
fill vs manual fill
Comments
Confirm delete:
Do you really want to delete benchmark?