Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Filling an array
(version: 1)
Comparing performance of:
Regular vs Test
Created:
one year ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<!--your preparation HTML code goes here-->
Script Preparation code:
/*your preparation JavaScript code goes here To execute async code during the script preparation, wrap it as function globalMeasureThatScriptPrepareFunction, example:*/ async function globalMeasureThatScriptPrepareFunction() { // This function is optional, feel free to remove it. // await someThing(); }
Tests:
Regular
const position = "Midfielder" Array(100).fill(position).map((p) => [crypto.randomUUID(), p])
Test
const position = "Midfielder" Array.from({ length: 100 }, () => [crypto.randomUUID(), position])
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Regular
Test
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:134.0) Gecko/20100101 Firefox/134.0
Browser/OS:
Firefox 134 on Mac OS X 10.15
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Regular
24307.1 Ops/sec
Test
23712.8 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The benchmark you provided compares two methods for filling and transforming an array in JavaScript, specifically focusing on different approaches to create an array filled with a specific value and then returning an array of tuples. ### Test Cases Explained: 1. **Regular Method**: ```javascript const position = "Midfielder"; Array(100).fill(position).map((p) => [crypto.randomUUID(), p]); ``` - In this method, an array of length 100 is created and filled with the string "Midfielder". The `map` function is then used to transform each element into a tuple that consists of a unique identifier (generated using `crypto.randomUUID()`) and the position. - **Pros**: - Simple and straightforward for users familiar with `map`. - `Map` provides a functional and expressive way to transform arrays. - **Cons**: - It involves two operations: first filling the array and then transforming it, which may lead to higher computational overhead, especially for larger arrays. 2. **Test Method**: ```javascript const position = "Midfielder"; Array.from({ length: 100 }, () => [crypto.randomUUID(), position]); ``` - This method creates a new array of length 100 using `Array.from`, where each element is generated on-the-fly with a tuple containing a unique identifier and the position. - **Pros**: - Only one operation is needed, as `Array.from` takes care of both creation and assignment in one step. - It can be more memory-efficient as values are generated dynamically. - **Cons**: - Slightly less intuitive for those who are more familiar with traditional array methods like `fill` and `map`. ### Performance Results: - The test results indicate the number of executions per second for each method: - **Regular Method**: Approximately 24,307 executions per second. - **Test Method**: Approximately 23,712 executions per second. The Regular Method outperformed the Test Method by a slight margin in this particular test scenario, although both approaches delivered reasonably high execution rates. ### Other Considerations: - **Library and Features**: The benchmark leverages the `crypto` API from the Web Cryptography API to generate unique identifiers with `crypto.randomUUID()`. This is a built-in JavaScript feature that allows developers to create UUIDs without third-party libraries. - **JavaScript Syntax**: The use of `Array.from` is a modern JavaScript feature introduced in ES6 (ECMAScript 2015), which offers a concise way of constructing arrays. It is inherently more versatile than the older array construction methods. ### Alternatives: - **Using Loops**: A traditional `for` or `while` loop could be used to create and fill the array. This may lead to more verbose code, but can sometimes be more performant and easier to optimize based on the specific needs. - **`new Array()` in Combination with `forEach`**: It's possible to create an empty array and fill values using a forEach loop, but this method may lead to more complex, less maintainable code. - **`map` with Predefined Values**: Instead of using `fill`, a new array could be created directly from an existing array of values using `map`, which allows for custom initialization. Ultimately, the choice of method may depend on personal or team coding style preferences, the specific performance requirements of the application, and considerations for readability and maintainability of the code.
Related benchmarks:
reate array by lenght
Assigning new variable
Test array concat
Test array concat with larger array
213find vs findIndex vs some (Array prototype methods)
me vs chatgpt
new Array(length).fill vs Array.from({ length })
new Array(length).fill().map vs Array.from({ length }, callback)
Nullish vs If
Comments
Confirm delete:
Do you really want to delete benchmark?