var a = Array(6576);
a.fill(7);
a[48] = 1;
const v = a.splice(48, 1)[0];
a.splice(657, 0, v);
const v = a[48];
a.copyWithin(48, 49, 658);
a[657] = v;
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
splice | |
copyWithin |
Test name | Executions per second |
---|---|
splice | 63154.7 Ops/sec |
copyWithin | 326865.8 Ops/sec |
Let's break down the provided benchmark definition and test cases.
Benchmark Definition
The benchmark measures the performance of two different approaches to replace an element in an array: Array.prototype.splice()
and Array.prototype.copyWithin()
. The array being modified is created with 6576 elements, filled with the value 7, and then has its 48th element replaced with 1.
Script Preparation Code
The script preparation code creates an array a
with 6576 elements, fills it with the value 7, and sets the 48th element to 1.
var a = Array(6576);
a.fill(7);
a[48] = 1;
This code is executed before running each test case.
Options Compared
Two options are compared:
Array.prototype.splice()
: This method replaces an element at the specified index with the elements removed from the array.Array.prototype.copyWithin()
: This method copies elements from one position in the array to another, leaving the original elements in place.Pros and Cons
Array.prototype.splice()
: Array.prototype.copyWithin()
:splice()
.Other Considerations
Both methods have different performance characteristics due to the way they handle element access and array modifications.
Library Used
None are explicitly mentioned, but JavaScript's built-in array methods (splice()
and copyWithin()
) use the V8 engine's optimizations under the hood.
Special JS Feature/Syntax
No special features or syntax are used in this benchmark. It focuses on comparing two widely supported array methods.