Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
[forked] append scalar value Array.prototype.concat vs spread operator vs push
(version: 0)
Compare the new ES6 spread operator with the traditional concat() method and push
Comparing performance of:
Array.prototype.concat vs spread operator vs Push
Created:
6 years ago
by:
Guest
Jump to the latest result
Tests:
Array.prototype.concat
var params = [ "hello", true, 7 ]; params = params.concat([1]); params = params.concat([2]);
spread operator
var params = [ "hello", true, 7 ]; params = [...params, 1] params = [...params, 2]
Push
var params = [ "hello", true, 7 ]; params.push(1); params.push(2);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Array.prototype.concat
spread operator
Push
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Overview** The provided JSON represents a JavaScript microbenchmarking test created on MeasureThat.net. The benchmark compares the performance of three approaches for appending scalar values to an array: `Array.prototype.concat()`, the spread operator (`...`), and `push()`. **Benchmark Definition** The benchmark definition is specified in the top-level JSON object: ```json "Name": "[forked] append scalar value Array.prototype.concat vs spread operator vs push" ``` This indicates that the benchmark is a fork of an existing test, modified to focus on appending scalar values to an array. **Test Cases** There are three individual test cases: 1. **`Array.prototype.concat`**: This test case uses the `concat()` method to append two scalar values (1 and 2) to an initial array (`params`) containing three elements: `"hello"`, `true`, and `7`. ```javascript var params = [ "hello", true, 7 ]; params = params.concat([1]); params = params.concat([2]); ``` 2. **Spread Operator (`...`)**: This test case uses the spread operator to append two scalar values (1 and 2) to an initial array (`params`) containing three elements: `"hello"`, `true`, and `7`. ```javascript var params = [ "hello", true, 7 ]; params = [...params, 1]; params = [...params, 2]; ``` 3. **`push()`**: This test case uses the `push()` method to append two scalar values (1 and 2) to an initial array (`params`) containing three elements: `"hello"`, `true`, and `7`. ```javascript var params = [ "hello", true, 7 ]; params.push(1); params.push(2); ``` **Library Usage** None of the test cases use any external libraries. **Special JS Features or Syntax** The spread operator (`...`) is a new feature introduced in ECMAScript 2015 (ES6). It allows for creating a new array from an existing array or other iterable by simply adding `...` before it. This syntax was not available in earlier JavaScript versions. **Pros and Cons of Each Approach** 1. **`Array.prototype.concat()`**: * Pros: Well-established method, easy to understand. * Cons: Creates a new array object, which can be inefficient for large datasets. 2. **Spread Operator (`...`)**: * Pros: Creates a shallow copy of the original array, avoiding unnecessary allocations. * Cons: Requires ECMAScript 2015 (ES6) support, may not work in older browsers or environments. 3. **`push()`**: * Pros: Modifies the existing array object, efficient for large datasets. * Cons: Does not create a new copy of the array, which can lead to unexpected behavior if mutated concurrently. **Other Alternatives** If you prefer using `Array.prototype.concat()` or `push()`, you could also consider using other methods, such as: * Using `Array.prototype.slice()` followed by `concat()`: `params = [...params.slice(), 1, 2]` * Using a library like Lodash's `concat` function: `params = _.concat(params, [1, 2])` However, the spread operator (`...`) is generally considered the most efficient and convenient approach for appending scalar values to an array.
Related benchmarks:
concat 2 arrays: Array.prototype.concat vs spread operator
Array.prototype.concat vs spread operator (add)
Array concat vs spread operator vs push for single values
Array.prototype.concat vs spread operator (fix)
Array.prototype.concat vs spread operator real
Comments
Confirm delete:
Do you really want to delete benchmark?