Skip to content

Instantly share code, notes, and snippets.

@caderek
Created January 5, 2025 20:30
Show Gist options
  • Save caderek/3e08e9ca29615a9eac832f9c2f2550c7 to your computer and use it in GitHub Desktop.
Save caderek/3e08e9ca29615a9eac832f9c2f2550c7 to your computer and use it in GitHub Desktop.
JS list vs array benchmark for adding and removing elements at the head.
import b from "benny"
const SIZE = 100_000
let list = {}
for (let i = SIZE; i > 0; i--) {
list = !list.value ? { value: i, rest: null } : { value: i, rest: list }
}
const arr = Array.from({ length: SIZE }, (_, i) => i + 1)
await b.suite(
`Head operations (${SIZE} elements initially)`,
b.add("list", () => {
list = list.rest
list = {
value: Math.random(),
rest: list,
}
}),
b.add("array", () => {
arr.shift()
arr.unshift(Math.random())
}),
b.cycle(),
b.complete(),
)
console.log(list.value, arr[0])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment