-
-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Performant queue #51
Open
JaoodxD
wants to merge
15
commits into
metarhia:master
Choose a base branch
from
JaoodxD:performant-queue
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Performant queue #51
Changes from 12 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
9ac6dcb
add performant queue implementation
JaoodxD 50df4c9
fix newline format
JaoodxD 86e7e15
add iterator to match array spreading behavior
JaoodxD 8963a73
update all array queues to use implemented performant queue
JaoodxD 2dfaa8d
remove unnecessary code
JaoodxD cbc3dfb
add queue performance test
JaoodxD 440f17d
add queue perf test
JaoodxD 1cae25d
fix prettify
JaoodxD 7fe2b46
fix constructor name and number delimeter
JaoodxD 96f0f34
update benchmark to make iteration count configurable
JaoodxD 1586804
remove unnecessary std output
JaoodxD fa34b3a
add `lib/queue.js` to package files list
JaoodxD 40cfe0b
Update `Queue` import to make it explicit
JaoodxD 3c68761
Update `Queue` import to make it explicit
JaoodxD 41f2d42
Fix `lib/queue.js` not being part of the installable package
JaoodxD File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
'use strict'; | ||
|
||
class Queue { | ||
constructor() { | ||
this.elements = []; | ||
this.offset = 0; | ||
} | ||
|
||
push(...element) { | ||
this.elements.push(...element); | ||
return this; | ||
} | ||
|
||
shift() { | ||
if (this.length === 0) return null; | ||
const first = this.elements[this.offset]; | ||
this.offset++; | ||
|
||
if (this.offset * 2 < this.elements.length) return first; | ||
|
||
this.elements = this.elements.slice(this.offset); | ||
this.offset = 0; | ||
return first; | ||
} | ||
|
||
get length() { | ||
return this.elements.length - this.offset; | ||
} | ||
|
||
*[Symbol.iterator]() { | ||
for (let i = this.offset; i < this.length; i++) { | ||
yield this.elements[i]; | ||
} | ||
} | ||
} | ||
|
||
module.exports = Queue; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,7 +46,7 @@ | |
"email": "[email protected]" | ||
}, | ||
"main": "web-locks.js", | ||
"files": [], | ||
"files": ["./lib/queue.js"], | ||
"engines": { | ||
"node": ">=11.0.0" | ||
}, | ||
|
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
'use strict'; | ||
|
||
const assert = require('node:assert/strict'); | ||
const { performance } = require('node:perf_hooks'); | ||
const Queue = require('../lib/queue'); | ||
|
||
const simpleQueueBench = (QueueClass) => { | ||
const queue = new QueueClass(); | ||
const iterations = 100000; | ||
let sum = 0; | ||
|
||
const timeStart = performance.now(); | ||
for (let i = 0; i < iterations; i++) { | ||
queue.push(() => i); | ||
} | ||
for (let i = 0; i < iterations; i++) { | ||
sum += queue.shift()(); | ||
} | ||
const timeEnd = performance.now(); | ||
|
||
const execTime = timeEnd - timeStart; | ||
return { sum, execTime }; | ||
}; | ||
|
||
module.exports = async () => { | ||
const testedClasses = [Array, Queue]; | ||
|
||
const [arrayResult, queueResult] = testedClasses.map(simpleQueueBench); | ||
const { sum: arraySum, execTime: arrayExecTime } = arrayResult; | ||
const { sum: queueSum, execTime: queueExecTime } = queueResult; | ||
|
||
const isQueueFaster = arrayExecTime > queueExecTime; | ||
|
||
assert.strictEqual(queueSum, arraySum); | ||
assert.strictEqual(isQueueFaster, true); | ||
}; |
This file contains 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It may be better to use enqueue/dequeue methods and under the hood add elements with array.unshift, and remove elements with offset/slice. Need to check performance and compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This approach was to match current array methods (push/shift) to minimize refactoring, but I agree that enqueue/dequeue could be more convinient.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also here is
test/queue-performance.js
performance comparison test to compare queues of 100k elements. It shows ~25x execution time difference. This factor is growing with queue size e.g. ~800x at 500k elements.