<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
</body>
</html>
list = document.querySelectorAll('p');
Array.from(list);
[list]
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Array#from | |
Spread Operator |
Test name | Executions per second |
---|---|
Array#from | 5852.8 Ops/sec |
Spread Operator | 7225.5 Ops/sec |
Let's break down the provided benchmark definition and test cases.
Benchmark Definition
The benchmark definition is represented by the JSON object that describes the task to be measured. In this case, it's "creating array from array like objects".
The script preparation code list = document.querySelectorAll('p');
creates an HTML list of 10 paragraph elements (<p>
) and assigns them to a variable named list
.
Options Compared
Two options are compared:
document.querySelectorAll()
array) and returns a new array with the same elements.Pros and Cons
Array.from():
Pros:
document.querySelectorAll()
)Cons:
Spread Operator:
Pros:
Cons:
Library
The document.querySelectorAll()
function is a part of the DOM (Document Object Model) API, which is a library of APIs that allow you to interact with and manipulate HTML documents.
Special JS Feature/Syntax
None mentioned explicitly, but note that the spread operator was introduced in ES6, so browsers using older versions of ES may not support it.