var arr = [1, 2, 3];
var other = arr.concat([99]);
var other = arr.push(99);
var other = arr.splice(arr.length-1, 0, 99);
var other = [ arr, 99 ]
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Array.prototype.concat | |
Array.prototype.push | |
Array.prototype.splice | |
spread operator |
Test name | Executions per second |
---|---|
Array.prototype.concat | 2983940.0 Ops/sec |
Array.prototype.push | 8085965.0 Ops/sec |
Array.prototype.splice | 3241472.2 Ops/sec |
spread operator | 0.3 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks!
What is being tested?
The provided JSON represents a set of test cases for measuring the performance of various array methods in JavaScript: concat
, push
, and the spread operator (...
). The tests are designed to compare the execution times of these three methods when used with different arrays.
Options compared:
Array.prototype.concat()
: This method creates a new array by copying elements from an existing array.Array.prototype.push()
: This method adds one or more elements to the end of an array and returns the new length of the array....
): This is a shorthand syntax for creating a new array from existing elements.Pros and cons:
concat()
:push()
:...
):Library usage:
None of the provided test cases use any external libraries. The focus is solely on measuring the performance of JavaScript's built-in array methods.
Special JS feature/syntax:
The spread operator (...
) is a relatively recent addition to JavaScript, introduced in ECMAScript 2018 (ES2018). It allows for concise array creation and modification.
Benchmark preparation code:
The script preparation code var arr = [1, 2, 3];
creates an initial array of three elements. Each test case then performs a specific operation on this array to measure the performance of the corresponding array method.
Other alternatives:
To further improve benchmarking and testing, you could consider adding more test cases, such as:
By expanding the scope of your benchmarking effort, you can gain a more comprehensive understanding of JavaScript's array methods and their performance characteristics.