Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Create Array Filled with Undefined
(version: 0)
Comparing performance of:
Array.apply vs Array.concat vs Array.fill vs Custom function
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var length = 10000;
Tests:
Array.apply
const array = Array.apply(null, { length });
Array.concat
const array = [].concat.apply([], { length });
Array.fill
const array = []; array.length = length; array.fill(undefined);
Custom function
const func = (...args) => args; func.apply(null, { length });
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Array.apply
Array.concat
Array.fill
Custom function
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36
Browser/OS:
Chrome 102 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Array.apply
3652.1 Ops/sec
Array.concat
3052.9 Ops/sec
Array.fill
36809.0 Ops/sec
Custom function
3946.3 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down what's being tested in the provided JSON benchmark. **Benchmark Definition:** The benchmark defines how to create an array filled with `undefined` values. There are four different approaches: 1. **`Array.apply(null, { length })`**: This method creates a new array object and then applies the `length` property to it using the `apply()` method. 2. **`Array.concat.apply([], { length })`**: This method uses the `concat()` method to create an empty array, and then applies the `length` property to it using the `apply()` method. 3. **`Array.fill(undefined)`**: This method simply sets all elements of the existing array to `undefined` using the `fill()` method. 4. **Custom function (`const func = (...args) => args; func.apply(null, { length });`)**: This is a custom function that creates an empty array and then applies the `length` property to it using the `apply()` method. **Options compared:** The benchmark compares the performance of these four approaches: * **`Array.fill()`**: A simple and efficient way to create an array filled with values. * **Custom function**: A custom implementation that creates an empty array and then applies the `length` property to it using the `apply()` method. **Pros and Cons:** 1. **`Array.fill(undefined)`**: * Pros: Simple, efficient, and widely supported (all modern browsers). * Cons: May not be suitable for large arrays or performance-critical applications. 2. **Custom function (`const func = (...args) => args; ...`)**: * Pros: Allows for fine-grained control over array creation and can be optimized for specific use cases. * Cons: Requires more code and may have additional overhead due to the custom implementation. **Library/Functionality used:** None of the approaches require any external libraries or functions beyond what's built into JavaScript. **Special JS features or syntax:** None of the approaches rely on any special JavaScript features or syntax. However, it's worth noting that `Array.fill()` is a relatively modern feature (introduced in ECMAScript 2015) and may not be supported in older browsers. **Other alternatives:** If you need to create an array filled with values for performance-critical applications, you could also consider using: * **`Buffer.alloc()`**: For creating a buffer filled with zeros or other values. * **`TypedArray` constructors**: Such as `Int8Array`, `Uint8Array`, etc., which can be used to create arrays of specific types and sizes. Keep in mind that these alternatives may have different performance characteristics and use cases compared to the approaches tested in this benchmark.
Related benchmarks:
Array creation
empty an array in JavaScript?
teststest
teststest1
Creating arrays with specified length
Comments
Confirm delete:
Do you really want to delete benchmark?