function createLinkedListQueue() {
let length = 0
let head = undefined
let tail = undefined
return {
enqueue(value) {
enqueueNode(value)
},
dequeue() {
return dequeueNode()
},
size() {
return length
},
head() {
return head
},
tail() {
return tail
}
}
function enqueueNode(value) {
const node = {
value,
next: undefined
}
if(!head) {
head = node
tail = node
}
else {
tail.next = node
tail = node
}
length += 1
}
function dequeueNode() {
if(head) {
const value = head.value
head = head.next
length -= 1
return value
}
tail = undefined
return undefined
}
}
function createDoubleArrayQueue() {
return {
input: [],
output: [],
enqueue(element) {
this.input.push(element)
},
dequeue() {
if(this.output.length === 0) {
this.pivot()
}
return this.output.pop()
},
pivot() {
this.output = this.input.reverse()
this.input = []
},
isEmpty() {
return this.input.length === 0 && this.output.length === 0
}
}
}
var doubleQ = createDoubleArrayQueue()
var linkedListQ = createLinkedListQueue()
var queueSize = 1000000
for(let i = 0; i < queueSize; i++) {
doubleQ.enqueue('testString' + i)
linkedListQ.enqueue('testString' + i)
}
for(let i = 0; i < queueSize; i++) {
doubleQ.dequeue()
}
for(let i = 0; i < queueSize; i++) {
linkedListQ.dequeue()
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Double Array | |
Linked List |
Test name | Executions per second |
---|---|
Double Array | 16.7 Ops/sec |
Linked List | 296.6 Ops/sec |
I'll break down the explanation into manageable chunks.
Benchmark Overview
The benchmark is designed to compare the performance of two queue data structures: a linked list queue and a double array queue. The test aims to evaluate which implementation performs better under different scenarios.
Script Preparation Code
The script preparation code consists of two main functions:
createLinkedListQueue()
: This function creates a linked list queue implementation with methods for enqueueing, dequeueing, and checking the size.createDoubleArrayQueue()
: This function creates a double array queue implementation with methods for enqueueing, dequeueing, and checking if the queue is empty.Benchmark Definition
The benchmark definition consists of two test cases:
**: This test case executes a loop that dequeues an element from the double array queue
1000000` times.**: This test case executes a loop that dequeues an element from the linked list queue
1000000` times.Library and Framework
The benchmark uses two libraries:
Special JS Feature or Syntax
There are no special JavaScript features or syntax used in this benchmark.
Pros and Cons of Approaches
Here's a brief summary of the pros and cons of each approach:
Other Considerations
When choosing between these two approaches, consider the following factors:
Alternatives
Other queue data structures that could be used for this benchmark include:
However, the linked list queue and double array queue implementations provide a good trade-off between performance, memory usage, and implementation complexity.
Keep in mind that the choice of queue data structure ultimately depends on the specific requirements and constraints of your application or use case.