Created
January 5, 2025 20:30
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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