Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array creation
(version: 0)
Comparing performance of:
Array from vs Array loop vs Array(length).fill
Created:
9 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
const length = 100000
Tests:
Array from
function arrayFrom() { const array = Array.from({length: length}, x => x = true) }
Array loop
function arrayLoop() { const array = []; for (let i = 0; i < length; i++) array.push(true); }
Array(length).fill
function arrayFill() { const array = Array(length).fill(true); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Array from
Array loop
Array(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):
**Benchmark Overview** The provided benchmark measures the performance of three different ways to create an array in JavaScript: `Array.from()`, using a traditional `for` loop (`arrayLoop()`), and using the `Array(length).fill(true)` syntax. **Script Preparation Code** The script preparation code sets the length of the array to be created, which is 100,000. This value is used consistently across all three benchmark definitions. **Html Preparation Code** There is no HTML preparation code provided, which means that the benchmark results are likely being generated in a headless browser environment (e.g., Chrome Headless). **Individual Test Cases** The individual test cases are defined as follows: 1. **Array from** ```javascript function arrayFrom() { const array = Array.from({length: length}, x => x = true); } ``` This code uses the `Array.from()` method to create an array with the specified length. The callback function `x => x = true` is used to initialize each element of the array. 2. **Array loop** ```javascript function arrayLoop() { const array = []; for (let i = 0; i < length; i++) array.push(true); } ``` This code uses a traditional `for` loop to create an array with the specified length. The `push()` method is used to add each element to the array. 3. **Array(length).fill()** ```javascript function arrayFill() { const array = Array(length).fill(true); } ``` This code uses the `Array(length)` constructor and the `fill()` method to create an array with the specified length, filled with a value of `true`. **Pros and Cons** Here are some pros and cons of each approach: 1. **Array from** * Pros: + Concise and expressive code + No need to worry about indexing or bounds checking * Cons: + May be slower due to the use of a callback function + Requires support for modern JavaScript features (e.g., arrow functions) 2. **Array loop** * Pros: + Well-established and widely supported syntax + Easy to understand and optimize * Cons: + More verbose code compared to `Array.from()` + May require bounds checking or indexing to access elements 3. **Array(length).fill()** * Pros: + Fast and efficient, since it uses a native method + Can be optimized for performance by using specialized hardware (e.g., SIMD instructions) * Cons: + Limited flexibility, as the value being filled is fixed (`true`) + May not work in older browsers or environments that don't support `Array(length)` **Library and Special JS Features** There are no libraries mentioned in this benchmark. However, some browsers may have special JavaScript features or extensions enabled (e.g., WebAssembly, GPU acceleration), which could potentially impact performance. **Other Alternatives** Some alternative ways to create an array in JavaScript include: 1. Using the `Array()` constructor with a variable length ```javascript const array = new Array(length); ``` 2. Using the `Array.prototype.fill()` method (which is similar to `Array(length).fill()`) ```javascript const array = []; array.length = length; array.fill(true); ``` 3. Using a library like Lodash or Ramda, which provide functional programming utilities for creating arrays. It's worth noting that the choice of array creation method depends on the specific requirements and constraints of your application.
Related benchmarks:
Does presetting the size of an array make a difference?
Array with and without predefined size
Array with and without predefined sizes
push vs set length and index
array.length === 0 vs !!array.length vs array.length
Comments
Confirm delete:
Do you really want to delete benchmark?