Array.apply(null, new Array(10)).map(() => false);
Array.from({ length: 10}, () => false);
new Array(10).map(() => false);
const r = [];
for (let i = 0; i < 10; i++) r[i] = false;
const r = [];
for (let i = 0; i < 10; i++) r.push(false);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Array.apply | |
Array.from | |
map | |
Loop and assign | |
Loop and push |
Test name | Executions per second |
---|---|
Array.apply | 1578336.4 Ops/sec |
Array.from | 707714.4 Ops/sec |
map | 4302881.0 Ops/sec |
Loop and assign | 24591278.0 Ops/sec |
Loop and push | 23934880.0 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks!
Benchmark Overview
The provided benchmark measures the performance of different methods to create an array in JavaScript: Array.apply
, Array.from
, map
, and two looping approaches (Loop and assign
and Loop and push
). The goal is to determine which method is the fastest.
Method Comparison
Here's a brief overview of each method:
Array.apply(null, new Array(10)).map(() => false)
: This method uses Array.apply
to create an array with 10 elements, then maps over it to return false
. The resulting array is not used; it's only iterated over.Array.from({ length: 10 }, () => false)
: This method creates a new array using the Array.from()
constructor, which takes an iterable as an argument and returns a new array with the same elements. In this case, it creates an array of 10 falsy values.new Array(10).map(() => false)
: Similar to the first method, but uses new Array()
to create an empty array, then maps over it to return false
.const r = []; for (let i = 0; i < 10; i++) r[i] = false;
: This is a looping approach that creates an array by iterating over a range of indices and assigning values to the array elements.const r = []; for (let i = 0; i < 10; i++) r.push(false);
: Another looping approach, but uses push()
to add elements to the end of the array.Pros and Cons
Here's a brief summary of each method:
Array.apply
: Fast, as it avoids creating an intermediate array. However, it requires null to be used as the first argument.Array.from
: Creates a new array with a specified length, which can be beneficial for performance. However, it's not supported in older browsers and may incur a slight overhead due to the constructor call.map
: Similar to apply
, but creates an intermediate array that needs to be iterated over.Loop and assign
: Slow, as it involves iterating over indices and assigning values to elements. However, it's a simple and widely supported approach.Loop and push
: Similar to the first looping approach, but uses push()
instead of indexing.Library Usage
There is no explicit library usage in this benchmark. However, Array.from()
relies on the Array
constructor and some browser-specific features (e.g., length
property).
Special JS Features or Syntax
None are explicitly used in this benchmark.
Other Considerations
Alternatives
If you're looking for alternatives or variations on these methods, consider:
Array.prototype.slice()
instead of map
.Array.prototype.concat()
, Array.prototype.push()
, or Set
constructors.for...of
loop or reduce()
.