Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Create 2D Array
(version: 0)
Create a 2D "square" array that is filled with arrays of the same length.
Comparing performance of:
While Loop with declaration vs While Loop with Array.from vs While Loop with Push and Array.From vs While Loop with Push vs Array.from with map
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var size = 15;
Tests:
While Loop with declaration
const array = []; array.length = size; let index = array.length; while (index-- > 0) { const temp = []; temp.length = size; array[index] = temp; }
While Loop with Array.from
const array = []; array.length = size; let index = array.length; const temp = []; temp.length = size; while (index-- > 0) array[index] = Array.from(temp);
While Loop with Push and Array.From
const array = []; array.length = size; let index = array.length; const temp = []; temp.length = size; while (index-- > 0) array.push(Array.from(temp));
While Loop with Push
const array = []; array.length = size; let index = size; while (index-- > 0) { const temp = []; temp.length = size; array.push(temp); }
Array.from with map
const array = Array.from(Array(size), () => { const temp = []; temp.length = size; return temp; });
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (5)
Previous results
Fork
Test case name
Result
While Loop with declaration
While Loop with Array.from
While Loop with Push and Array.From
While Loop with Push
Array.from with map
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):
The provided JSON represents a benchmark for creating a 2D array in JavaScript using different approaches. Let's break down each test case and compare the options. **Benchmark Definition** The benchmark definition describes creating a 2D "square" array filled with arrays of the same length. **Script Preparation Code** The script preparation code sets the size of the 2D array to be created: `var size = 15;`. **Html Preparation Code** There is no HTML preparation code provided. **Individual Test Cases** 1. **While Loop with Declaration** This test case uses a traditional while loop with variable declaration: ```javascript let index = array.length; while (index-- > 0) { const temp = []; temp.length = size; array[index] = temp; } ``` Pros: Simple and straightforward. Cons: May be slower due to the use of `array[index]`, which can lead to a lot of lookups, and the variable declaration can lead to more memory allocation. 2. **While Loop with Array.from** This test case uses an array from method to create the inner arrays: ```javascript let index = array.length; while (index-- > 0) { const temp = []; temp.length = size; array[index] = Array.from(temp); } ``` Pros: More concise and expressive than the traditional while loop. Cons: May be slower due to the use of `Array.from`, which can lead to more memory allocation, and it may not perform as well for large arrays. 3. **While Loop with Push and Array.From** This test case uses a while loop with push and array from method: ```javascript let index = size; while (index-- > 0) { const temp = []; temp.length = size; array.push(Array.from(temp)); } ``` Pros: More concise than the traditional while loop. Cons: May be slower due to the use of `array.push`, which can lead to more memory allocation, and it may not perform as well for large arrays. 4. **While Loop with Push** This test case uses a while loop with push: ```javascript let index = size; while (index-- > 0) { const temp = []; temp.length = size; array.push(temp); } ``` Cons: May be slower due to the use of `array.push`, which can lead to more memory allocation, and it may not perform as well for large arrays. 5. **Array.from with map** This test case uses an array from method with a map: ```javascript const array = Array.from(Array(size), () => { const temp = []; temp.length = size; return temp; }); ``` Pros: Most concise and expressive, as it uses a single line of code. Cons: May be slower due to the use of `Array.from` with map, which can lead to more memory allocation, and it may not perform as well for large arrays. **Library** In this benchmark, the `Array.from()` method is used in three test cases. The purpose of `Array.from()` is to create a new array by mapping an existing iterable (such as an array or string) to a new array with the specified length. **Special JS Features** There are no special JavaScript features mentioned in this benchmark. **Other Alternatives** In addition to the five test cases provided, other alternatives for creating a 2D array could include: * Using `Array.fill()` and `map()`: `array.map((_, i) => Array(size).fill().map(() => []))` * Using `Array.prototype.reduce()`: `array.reduce((acc, _, i) => ({ ...acc, [i]: Array(size).fill().map(() => []) }), {})` These alternatives may have different performance characteristics compared to the test cases provided.
Related benchmarks:
Array creation types
teststest
Creating arrays with specified length
Test array and unshift
11111111
Comments
Confirm delete:
Do you really want to delete benchmark?