<section>
<p>Lobo-cinzento (nome científico:Canis lupus) é uma espécie de mamífero canídeo do gênero Canis. É um sobrevivente
da Era do Gelo, originário do Pleistoceno Superior, cerca de 300 mil anos atrás. É o maior membro remanescente
selvagem da família canidae.</p>
<p>Os lobos-cinzentos são tipicamente predadores ápice nos ecossistemas que ocupam. Embora não sejam tão adaptáveis
à
presença humana como geralmente ocorre com as demais.</p>
<p>O peso e tamanho dos lobos variam muito em todo o mundo, tendendo a aumentar proporcionalmente com a latitude.</p>
<p>Os lobos são capazes de percorrer longas distâncias com uma velocidade média de 10 quilômetros por hora e são
conhecidos por.</p>
</section>
const paragrafos = document.querySelectorAll('section p');
const arrayParagrafos = Array.prototype.slice.call(paragrafos);
const paragrafos = document.querySelectorAll('section p');
const arrayParagrafos = Array.from(paragrafos);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
slice.call | |
Array.from |
Test name | Executions per second |
---|---|
slice.call | 152800.7 Ops/sec |
Array.from | 147407.8 Ops/sec |
Measuring the performance of JavaScript microbenchmarks can be a complex task, as it depends on various factors such as the JavaScript engine, browser version, and system configuration.
The provided benchmark definition json represents two test cases:
slice.call()
Array.from()
Both tests are designed to measure the performance of converting an HTML collection of paragraph elements to an array using different methods.
Options compared:
slice.call()
:slice()
function from the Array.prototype
and calls it with the call()
method to convert the HTML collection to an array.slice()
function returns a new array with the specified elements, effectively "cutting" the original collection into an array.Array.from()
: from()
function from the Array
constructor and passes the HTML collection as an argument. It creates a new array with all elements from the collection.Pros and cons of each approach:
slice.call()
call()
method.Array.from()
slice.call()
.for...of
loop).Array
methods.Library and its purpose:
Special JS feature/syntax:
There are no special JavaScript features or syntaxes used in these benchmarks. The focus is solely on comparing the performance of two array conversion methods.
Other alternatives:
Other array conversion methods that could be considered as alternatives include:
Array.prototype.map()
and collecting the results into an array.Promise.all()
with a promise for each element in the collection.Keep in mind that these alternative approaches might have different performance characteristics, readability, and compatibility issues depending on the specific use case.