Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Fill array with random integers
(version: 0)
Fill Array(1000) with random 1 - 999999 inclusive
Comparing performance of:
Set-length array vs Array.push
Created:
7 years ago
by:
Guest
Jump to the latest result
Tests:
Set-length array
let arr = Array(1000).fill(undefined).map(e => Math.floor(Math.random()*1000000+1));
Array.push
let arr = []; for (i=0; i<1000001; i++){arr.push(Math.floor(Math.random()*1000000+1))}
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Set-length array
Array.push
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 dive into the world of MeasureThat.net and explore what's being tested in this benchmark. **Benchmark Definition** The benchmark definition is a JSON object that provides metadata about the test case. In this case, there are two benchmark definitions: 1. `Fill array with random integers`: This benchmark definition describes the test scenario, where an array of 1000 elements is filled with random integers between 1 and 999,999 (inclusive). 2. Script Preparation Code: The script preparation code is empty (`null`), which means that no specific code needs to be executed before running the test. 3. HTML Preparation Code: The HTML preparation code is also empty (`null`), indicating that no special HTML setup is required. **Individual Test Cases** There are two individual test cases: 1. `Set-length array`: This test case uses a concise syntax to create an array of 1000 elements using `Array(1000).fill(undefined).map(e => Math.floor(Math.random()*1000000+1))`. 2. `Array.push`: This test case uses a traditional `for` loop to push 1000 random integers onto an empty array (`let arr = [];\r\nfor (i=0; i<1000001; i++){arr.push(Math.floor(Math.random()*1000000+1))}`). **Library and Purpose** Neither of the test cases uses any external libraries. The `Array` constructor is a built-in JavaScript function, and the `Math.random()` function is also a built-in function. **Special JS Features or Syntax** There are no special JavaScript features or syntax being used in either test case. **Pros and Cons of Different Approaches** 1. **Set-length array**: * Pros: Concise and efficient way to create an array. * Cons: May not be as intuitive for developers who are unfamiliar with the `fill()` method. 2. **Array.push**: * Pros: Traditional and well-understood approach that many developers will be familiar with. * Cons: Less concise and potentially less efficient than the set-length array approach. **Other Alternatives** If you were to create a similar benchmark, other alternatives for the `set-length array` approach could include: * Using `Array.from()`: `Array.from(new Array(1000), () => Math.floor(Math.random()*1000000+1))` * Using a library like Lodash's `repeat()` function: `_._.repeat(1000)(Math.floor(Math.random()*1000000+1))` For the `Array.push` approach, alternative methods could include: * Using `splice()`: `let arr = []; for (i=0; i<1000001; i++){arr.splice(i, 0, Math.floor(Math.random()*1000000+1))}` * Using a library like Lodash's `repeat()` function with the `push()` method: `_._.forEach(1000)(arr.push, Math.floor(Math.random()*1000000+1))` Keep in mind that these alternatives may not necessarily change the outcome of the benchmark, but they could provide additional insights into different aspects of JavaScript performance.
Related benchmarks:
LIS-test2
Array.Sort vs Math.Min-Max
Fisher-Yates Shuffle
Set.has v.s Array.includes
yoooooo
Comments
Confirm delete:
Do you really want to delete benchmark?