var attrs = document.body.querySelector('.modal').attributes
var other = Array.prototype.slice.call(attrs);
var other = [ attrs ]
var other = [];
[].push.apply(other, attrs)
var other = Array.from(attrs);
var other = Object.values(attrs);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
slice | |
spread operator | |
push apply into empty array | |
Array.from | |
Object.values |
Test name | Executions per second |
---|---|
slice | 365786.8 Ops/sec |
spread operator | 430096.5 Ops/sec |
push apply into empty array | 1110369.9 Ops/sec |
Array.from | 317905.8 Ops/sec |
Object.values | 75552.5 Ops/sec |
Let's dive into the provided benchmark.
The benchmark is designed to compare four different methods of converting an HTML attributes
list into an array:
[ ...attrs ]
)Array.prototype.slice.call(attrs)
Array.from(attrs)
Object.values(attrs)
Here's a brief overview of each method:
[ ...attrs ]
): The spread operator allows you to extract elements from an array-like object (such as attrs
) and create a new array with those elements.Array.prototype.slice.call(attrs)
: This method creates a new array by calling the slice()
method on attrs
and then passing that result to call()
, which returns the resulting array. The call()
method is used to invoke a function with a given this value, but in this case, it's used to convert the array-like object into an actual array.Array.from(attrs)
: This method creates a new array from an iterable (such as attrs
). It does not require that the input be an array-like object, making it more flexible than some of the other options.Object.values(attrs)
: This method returns a new array containing all the values of the given object (attrs
). However, this will only include the attribute values if they are own properties of attrs
, not inherited from parent elements.Comparison Options
The benchmark is designed to compare these four methods in terms of performance. The test case that uses a library (in this case, no libraries are explicitly mentioned) and special JS features (Macintosh
as the device platform, Chrome 66
, etc.) helps to isolate the differences between these methods.
Pros and Cons
Here's a brief overview of each method:
Array.from()
or Object.values()
.Array.prototype.slice.call(attrs)
: Pros:call()
method.Array.from(attrs)
: Pros:Object.values(attrs)
: Pros:Array.from()
or spread operator for large arrays.Other Considerations
When choosing a method, consider the specific requirements of your project:
Array.from()
or other native methods might be better options.Object.values()
could be suitable.Alternative Methods
Some alternative methods for converting array-like objects into arrays include:
Array.prototype.reduce()
: While not as straightforward as the spread operator or Array.from()
, this method can create an array from an iterable in a more functional programming style.Array.prototype.map()
and concat()
: This approach involves creating a new array by mapping over the input array-like object and then concatenating the results. While it's possible, it's often less efficient than using native methods like Array.from()
.for...of
loop or Array.prototype.forEach()
: These methods can also be used to iterate over an array-like object, but they're typically more cumbersome than using a spread operator or Array.from()
.