function randomArray(e) {
e = e || 32;
var t = "ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678",
a = t.length,
n = [];
for (i = 0; i < e; i++) n.push(t.charAt(Math.floor(Math.random() * a)));
return n
}
var arr1 = randomArray(100)
var arr2 = randomArray(10000)
var arr3 = randomArray(1000000)
var result1 = arr1.splice(Math.floor(arr1.length / 2), 1, "i")
var result1 = arr2.splice(Math.floor(arr2.length / 2), 1, "i")
var result1 = arr3.splice(Math.floor(arr3.length / 2), 1, "i")
var result1 = arr1.toSpliced(Math.floor(arr1.length / 2), 1, "i")
var result1 = arr2.toSpliced(Math.floor(arr2.length / 2), 1, "i")
var result1 = arr3.toSpliced(Math.floor(arr3.length / 2), 1, "i")
var result1 = arr3.splice(Math.floor(arr3.length / 2), 1, "i", "2")
var result1 = arr3.splice(Math.floor(arr3.length / 2), 1)
var result1 = arr3.toSpliced(Math.floor(arr3.length / 2), 1, "i", "d")
var result1 = arr3.toSpliced(Math.floor(arr3.length / 2), 1)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
splice-1 | |
splice-2 | |
splice-3 | |
toSpliced-1 | |
toSpliced-2 | |
toSpliced-3 | |
splice-3 add items | |
splice-3 remove | |
toSpliced-3 add | |
toSpliced-3 remove |
Test name | Executions per second |
---|---|
splice-1 | 3758490.8 Ops/sec |
splice-2 | 3618269.8 Ops/sec |
splice-3 | 3762439.5 Ops/sec |
toSpliced-1 | 3089284.2 Ops/sec |
toSpliced-2 | 194754.6 Ops/sec |
toSpliced-3 | 448.6 Ops/sec |
splice-3 add items | 1251.2 Ops/sec |
splice-3 remove | 1758.3 Ops/sec |
toSpliced-3 add | 460.7 Ops/sec |
toSpliced-3 remove | 453.0 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks!
What is being tested?
MeasureThat.net is testing the performance of two methods: splice
and toSpliced
. Both methods are used to remove or add elements from an array.
splice
method modifies the original array by removing or adding elements at a specified index.toSpliced
method creates a new array with the modified elements, leaving the original array unchanged.Options compared
The benchmark is comparing different variations of these two methods:
arr.splice(Math.floor(arr.length / 2), 1, "i")
: removes the middle element from the array and adds an element at that index.arr.toSpliced(Math.floor(arr.length / 2), 1, "i")
: creates a new array with the modified elements, leaving the original array unchanged.arr.splice(Math.floor(arr.length / 2), 1)
: removes the middle element from the array without adding anything.arr.toSpliced(Math.floor(arr.length / 2), 1)
: creates a new array with the modified elements, leaving the original array unchanged (without adding an element).arr.splice(0, 1)
and arr.splice(arr.length, 1)
: removes elements from both ends of the array.Performance results
The benchmark provides execution times for each test case, indicating that:
splice
is generally faster than creating a new array using toSpliced
.splice
is slower than creating a new array with only the added element.arr.splice(0, 1)
and arr.splice(arr.length, 1)
) are faster than removing or adding a single element in the middle.Conclusion
The results suggest that splice
is generally faster for removing or modifying elements in an array, while toSpliced
is more efficient when creating a new array with modified elements. The choice of method depends on the specific use case and performance requirements.