Closed jeswr closed 2 years ago
Yes—but the test above is benchmarking a situation that should never happen. It tests the pattern:
However, actual patterns will look like this:
So we'd need evidence that the performance gains translate to frequent additions/deletions.
Separately, it's good for V8 to have some warmup runs before measuring.
I created a quick example:
// 100 iterators, with 1,000,000 push/read sequences of 0–4 items
console.time('LinkedList');
for (let a = 0; a < 100; a++) {
const list = new LinkedList();
for (let i = 0; i < 1_000_000; i++) {
for (let j = randInt(0, 4); j > 0; j--)
list.push(i);
for (let j = randInt(0, 4); j > 0; j--)
list.shift();
}
}
console.timeEnd('LinkedList');
console.time('LinkedListNext');
for (let a = 0; a < 100; a++) {
const list = new LinkedListNext();
for (let i = 0; i < 1_000_000; i++) {
for (let j = randInt(0, 4); j > 0; j--)
list.push(i);
for (let j = randInt(0, 4); j > 0; j--)
list.shift();
}
}
console.timeEnd('LinkedListNext');
function randInt(min: number, max: number) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
which yields for me
LinkedList: 6.865s
LinkedListNext: 6.584s
And this includes the fact that LinkedListNext
keeps a lot of items in memory.
I'll close this for now until new evidence emerges.
Noting that the V8 engine has similar optimizations to firefox for up to 50_000 elements, we could do the following (~2x speedup).
Results LinkedListNext: 892.099ms LinkedList: 2.283s