Slice and assign vs splice in place vs set length

Benchmark created on


Description

Compare removing trailing array elements with assigning the variable to the slice of the old one without the trailing elements, vs mutating the array in place by removing the trailing elements from it, vs changing the array length to a smaller value

Test runner

Ready to run.

Testing in
TestOps/sec
.slice(0, -n)
let list = Array.from({ length: 10_000 }, Math.random)

list = list.slice(0, -500)
ready
.splice(L - n, n)
const list = Array.from({ length: 10_000 }, Math.random)

list.splice(list.length - 500, 500)
ready
.length -= n
const list = Array.from({ length: 10_000 }, Math.random)

list.length -= 500
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.