var s1 = "foo.bar";
var s2 = "foo";
var n1 = Array.from(s1)
var n2 = Array.from(s2)
var n1 = [s1];
var n2 = [s2];
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Array from | |
Spread |
Test name | Executions per second |
---|---|
Array from | 712707.9 Ops/sec |
Spread | 1049327.8 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks and explore what's being tested in this specific benchmark.
What is being tested?
The provided JSON represents two test cases: Array.from
and Spread syntax (using the spread operator). The goal is to measure the performance difference between these two approaches when splitting a string.
Options compared:
There are two options being compared:
...
) to create a new array by spreading the elements of an array-like object.Pros and Cons:
Array.from
.Other considerations:
When choosing between these two approaches, consider the following:
Array.from
, it might be more suitable to stick with that approach.Library usage:
There doesn't seem to be any external libraries used in this benchmark. The only library-like feature being tested is the Array.from
method itself.
Special JS features or syntax:
This benchmark uses a special JavaScript feature: the spread operator (...
). Introduced in ECMAScript 2015, it allows you to create new arrays by spreading elements of an array-like object.
Benchmark preparation code:
The script preparation code is very simple:
var s1 = "foo.bar";
var s2 = "foo";
These variables are created as string literals.
Individual test cases:
Each test case consists of a single benchmark definition, which defines the input strings s1
and s2
. The test names indicate that we're testing both Array.from
and Spread syntax:
Array from
var n1 = Array.from(s1);
var n2 = Array.from(s2);
var n1 = [...s1];
var n2 = [...s2];
Latest benchmark result:
The results show that the Spread syntax ([...s1]
and [...s2]
) outperformed the Array.from
method in this specific benchmark.
Other alternatives for creating arrays from iterables or array-like objects include:
map()
function with an arrow functionfor
loop