<div id="my-div"></div>
const div = document.getElementById("my-div");
for (let i = 0; i < 10000; i++) {
const child = document.createElement("div");
child.append(`I'm div-${i}`);
div.append(child);
}
const childNodes = [document.querySelectorAll("#my-div div")];
const childNodes = Array.from(document.querySelectorAll("#my-div div"));
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Spread | |
Array.from |
Test name | Executions per second |
---|---|
Spread | 800.5 Ops/sec |
Array.from | 842.4 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks and explore what's being tested on MeasureThat.net.
Benchmark Overview
The benchmark tests two approaches for creating an array from querySelectorAll
method results:
...
)Array.from()
methodThe test case creates a DOM element with 10,000 child elements and then uses querySelectorAll
to select all the child elements. The resulting array is created using either the spread operator or the Array.from()
method.
Options Compared
Two options are compared:
...
): This approach uses the syntax [...array]
to create a new array from an existing array.Array.from()
method to create a new array from an existing iterable.Pros and Cons
Here are some pros and cons of each approach:
Spread Operator (...
)
Pros:
Lightweight: Creating an array using the spread operator is generally faster than using Array.from()
.
Simple syntax: The syntax is easy to read and write. Cons:
Not as efficient for large arrays: The spread operator creates a new array by iterating over the elements, which can be slower for large arrays.
Array.from() Method Pros:
More flexible: Array.from()
allows you to pass any iterable (e.g., an array, string, or DOMNodeList) as an argument.
Cons:
Slightly heavier: Creating an array using Array.from()
may be slightly slower than using the spread operator.
Other Considerations
Both approaches have their own set of caveats:
Array.from()
is supported by most modern browsers, but some older browsers may not have it.Library and Syntax
In this benchmark, there are no external libraries used besides the JavaScript core features mentioned above. However, if you're interested in exploring other approaches, you might consider using a library like Lodash or Underscore.js, which provide various utility functions for working with arrays.
Special JS Features or Syntax
There are no special JavaScript features or syntax mentioned in this benchmark.
Now that we've explored the benchmark, let's take a look at some alternative approaches:
Array.from()
or the spread operator, you could use a for...of loop to create an array.const childNodes = [];
for (const child of document.querySelectorAll('#my-div div')) {
childNodes.push(child);
}
This approach is generally slower than using Array.from()
or the spread operator.
map()
to create a new array from an existing iterable.const childNodes = document.querySelectorAll('#my-div div').map((child) => child);
This approach is similar to using Array.from()
, but it uses the map()
method instead.
These alternatives are not typically faster or slower than the original approaches, but they can provide interesting variations on how to create an array from an existing iterable.