var array = Array.from({ length: 10000 }).map((val, i) => i);
var messages = array.slice().splice(9999, 1);
var messages = array.slice().slice(0, 9999);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Splice | |
Slice |
Test name | Executions per second |
---|---|
Splice | 103384.0 Ops/sec |
Slice | 53448.9 Ops/sec |
Let's break down the benchmark and explain what's being tested.
Benchmark Overview
The benchmark is designed to compare the performance of two different methods for deleting elements from an array: splice
and slice
. The goal is to measure which method is faster when deleting a large number of elements at a specific index.
Script Preparation Code
The script preparation code creates an array with 10,000 elements using Array.from()
and then maps over it to assign each element a unique index value. This creates an array where each element has a known position in the array.
Options being Compared
Two options are being compared:
splice()
to delete the last element at index 9999.slice()
to get a subset of elements from the original array, excluding the last element at index 9999.Pros and Cons of Each Approach
Library Used
In this benchmark, no library is explicitly mentioned. However, it's worth noting that both splice()
and slice()
are part of the built-in JavaScript API.
Special JS Feature or Syntax
There isn't any special JavaScript feature or syntax being used in this benchmark. It's a straightforward example of comparing two common array manipulation techniques.
Other Considerations
Alternative Approaches
There are other ways to delete elements from an array, such as:
However, these methods are not being tested in this benchmark.
I hope this explanation helps you understand what's being measured in this benchmark!