var params = [ "hello", true, 7, 3, 4 ];
var other = params.slice().sort().join(',');
var params = [ "hello", true, 7, 3, 4 ]
var other = [ params ].sort().join(',')
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Array.prototype.slice | |
spread operator |
Test name | Executions per second |
---|---|
Array.prototype.slice | 3617568.5 Ops/sec |
spread operator | 3471559.5 Ops/sec |
Overview
The provided JSON represents a benchmark test case on MeasureThat.net, which compares the performance of two approaches to create a sorted array from an existing one: using Array.prototype.slice
and then sorting, versus using the new ES6 spread operator (...
) with sorting.
Options Compared
There are only two options compared:
...
): This syntax creates a new array from an existing array by spreading its elements into a new array.Pros and Cons of Each Approach
slice()
method always returns a new array with the same length...
):Library
None of the provided test cases use a library. However, MeasureThat.net itself uses JavaScript as its programming language.
Special JS Feature/Syntax
The spread operator (...
) is a relatively new feature introduced in ECMAScript 2015 (ES6). It allows creating a new array from an existing array by spreading its elements into a new array.
Other Alternatives
In addition to the spread operator, other alternatives for creating a sorted array could include:
Array.prototype.concat()
and then sorting: var params = [ "hello", true, 7, 3, 4 ]; var other = params.concat().sort();
Array.prototype.reduce()
and then sorting: var params = [ "hello", true, 7, 3, 4 ]; var other = params.reduce((a, b) => a + ', ' + b).split(',').slice(1).sort();
However, these alternatives are not included in the provided benchmark test case.