var testArray = [83, 93, 27, 29, 2828, 234, 23, 56, 32, 56, 67, 77, 32, 45, 93, 17, 28, 83, 62, 99, 36, 28, 93, 27, 29, 2828, 234, 23, 56, 32, 56, 67, 77, 32, 45, 93, 17, 28, 83, 62, 99, 36, 28, 93, 27, 29, 2828, 234, 23, 56, 32, 56, 67, 77, 32, 45, 93, 17, 28, 83, 62, 99, 36, 28, 93, 27, 29, 2828, 234, 23, 56, 32, 56, 67, 77, 32, 45, 93, 17, 28, 83, 62, 99, 36, 28, 93, 27, 29, 2828, 234, 23, 56, 32, 56, 67, 77, 32, 45, 93, 17, 28, 83, 62, 99, 36, 28, 93, 27, 29, 2828, 234, 23, 56, 32, 56, 67, 77, 32, 45, 93, 17, 28, 83, 62, 99, 36, 28, 93, 27, 29, 2828, 234, 23, 56, 32, 56, 67, 77, 32, 45, 93, 17, 28, 83, 62, 99, 36, 28, 93, 27, 29, 2828, 234, 23, 56, 32, 56, 67, 77, 32, 45, 93, 17, 28, 83, 62, 99, 36, 28, 93, 27, 29, 2828, 234, 23, 56, 32, 56, 67, 77, 32, 45, 93, 17, 28, 83, 62, 99, 36, 28, 93, 27, 29, 2828, 234, 23, 56, 32, 56, 67, 77, 32, 45, 93, 17, 28, 83, 62, 99, 36, 28];
function getRandomInt(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min) + min); //The maximum is exclusive and the minimum is inclusive
}
const indexToRemove = getRandomInt(0, testArray.length)
const newArr = [
testArray.slice(0, indexToRemove),
testArray.slice(indexToRemove + 1)
]
const indexToRemove = getRandomInt(0, testArray.length)
const arrCopy = [testArray]
arrCopy.splice(indexToRemove, 1)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
spread | |
splice |
Test name | Executions per second |
---|---|
spread | 375717.8 Ops/sec |
splice | 494879.9 Ops/sec |
Let's break down the benchmark and its test cases.
Benchmark Overview
The benchmark measures the performance of two ways to remove an element from an array: using Array.prototype.splice()
(referred to as "splice") and using the spread operator (...
) to create a new array with the elements before and after the one to be removed (referred to as "spread").
Options Compared
The benchmark compares the performance of:
Array.prototype.splice()
to remove an element from the array....
) to create a new array with the elements before and after the one to be removed.Pros and Cons
Library Usage
None of the test cases use any external libraries.
Special JS Features/Syntax
...
) is used to create a new array with elements from an existing array.Other Considerations
The benchmark measures the performance of these two approaches in terms of executions per second, which indicates how many times each approach can be executed within a given time frame (e.g., 1 second).
Alternatives
If you're interested in exploring other methods for removing elements from an array, some alternatives include:
Keep in mind that each approach has its own use cases and trade-offs, so it's essential to choose the right method depending on your specific requirements.