<ul>
</ul>
var fooSet = new Set();
var ul = document.querySelector('ul');
for(var i=0;i<100;i++) {
fooSet.add(i);
ul.appendChild(document.createElement('li'));
}
var listItems = document.querySelectorAll('li');
var other = [].slice.call(fooSet);
var other = [].concat.call([],fooSet);
var other = Array.from(listItems);
var other = [listItems];
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Array.prototype.slice on NodeList | |
Array.prototype.concat on NodeList | |
Array.from on NodeList | |
Spread on NodeList |
Test name | Executions per second |
---|---|
Array.prototype.slice on NodeList | 14880080.0 Ops/sec |
Array.prototype.concat on NodeList | 9067304.0 Ops/sec |
Array.from on NodeList | 147007.5 Ops/sec |
Spread on NodeList | 148772.9 Ops/sec |
Benchmark Overview
The provided benchmark tests the performance of three different approaches for creating an array from a NodeList in modern JavaScript: Array.prototype.slice()
, Array.prototype.concat()
, and the spread operator (...
).
Benchmark Definition JSON Explanation
The benchmark definition JSON contains information about the test:
"Name"
: The name of the benchmark, which is "Array.prototype.slice vs Array.from vs Spread with Node List"."Description"
: No description is provided."Script Preparation Code"
: This code creates a new Set
object called fooSet
, selects an unordered list (ul
) from the DOM using document.querySelector()
, and populates it with 100 elements using a loop. Then, it creates three NodeList objects from the list items using different methods: Array.prototype.slice()
, Array.prototype.concat()
, and the spread operator (...
)."Html Preparation Code"
: This code generates an empty unordered list (<ul>
) in the HTML.Individual Test Cases
Each test case represents a single variation of creating an array from a NodeList:
**: Creates an array by slicing the
fooSetNodeList object using
Array.prototype.slice()`.**: Creates an array by concatenating two empty arrays using
Array.prototype.concat()and then passing the
fooSet` NodeList to it.**: Creates an array from the
listItemsNodeList object using
Array.from()`.**: Creates an array from the
listItems NodeList object using the spread operator (
...`).Library and Purpose
Set
, Array.prototype.slice()
, and Array.prototype.concat()
.Special JS Features or Syntax
The benchmark uses several modern JavaScript features:
Set
: A built-in data structure that stores unique values....
): Introduced in ECMAScript 2015 (ES6), it allows creating a new array from an existing iterable object.Pros and Cons of Each Approach
Array.prototype.slice()
:Array.prototype.concat()
:slice()
for large datasets due to the overhead of creating an empty array and concatenating elements to it....
):concat()
. It also allows creating arrays from other iterables like sets or maps.slice()
and concat()
, especially in older browsers.Other Alternatives
If you need to create an array from a NodeList without using these methods:
Array.from()
: This method is similar to the spread operator but creates a shallow copy of the original data.NodeList.prototype.map()
: This method applies a transformation function to each element in the NodeList and returns a new array.NodeList.prototype.forEach()
: This method iterates over the elements in the NodeList without creating an array, which can be useful if you need to process the data but don't need to create an array.Note that these alternatives may have different performance characteristics and use cases compared to the methods tested in this benchmark.