array vs linked list (last element) (v2)

Revision 2 of this benchmark created on


Setup

const arr = [];

class Link {
	prev = undefined;
	
	constructor(list, value){
		this.list = list;
		this.value = value;
	}	
}

class List {
	tail = undefined;
	
	push(value){
		const link = new Link(this, value);
		link.prev = this.tail;
		this.tail = link;
		return link;
	}
	
	pop(){
		this.tail = this.tail.prev;
	}
}

const list = new List();

class Hybrid {
	size = 0;
	arr = [];
	last = undefined;
	
	push(value){
		this.arr.push(value);
		this.size++;
		this.last = value;
	}
	
	pop(){
		this.arr.pop();
		this.size--;
		this.last = this.arr[this.size-1];
	}
}

const hybrid = new Hybrid();

class Hybrid2 {
	size = 0;
	arr = [];
	
	push(value){
		this.arr.push(value);
		this.size++;
	}
	
	pop(){
		this.arr.pop();
		this.size--;
	}
	
	last(){
		return this.arr[this.size-1];
	}
}

const hybrid2 = new Hybrid2();

const add = 1;
const get = 10;
let val;

Test runner

Ready to run.

Testing in
TestOps/sec
array
arr.length = 0; // clear
for(let i=0;i<add;i++){
	arr.push(Math.random());
}
for(let i=0;i<get;i++){
	// get last element
	val = arr[arr.length-1]+Math.random();
}
for(let i=0;i<add;i++){
	arr.pop();
}
ready
List
list.tail = undefined; // clear
for(let i=0;i<add;i++){
	list.push(Math.random());
}
for(let i=0;i<get;i++){
	// get last element
	val = list.tail.value+Math.random();
}
for(let i=0;i<add;i++){
	list.pop();
}
ready
Hybrid
hybrid.arr.length = 0;hybrid.size = 0;hybrid.last=undefined; // clear
for(let i=0;i<add;i++){
	hybrid.push(Math.random());
}
for(let i=0;i<get;i++){
	// get last element
	val = hybrid.last+Math.random();
}
for(let i=0;i<add;i++){
	hybrid.pop();
}
ready
Hybrid 2
hybrid2.arr.length = 0;hybrid.size=0;//clear
for(let i=0;i<add;i++){
	hybrid2.push(Math.random());
}
for(let i=0;i<get;i++){
	// get last element
	val = hybrid2.last()+Math.random();
}
for(let i=0;i<add;i++){
	hybrid2.pop();
}
ready

Revisions

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