brianc / node-postgres

PostgreSQL client for node.js.
https://node-postgres.com
MIT License
12.33k stars 1.23k forks source link

Pool uses a js array as a queue #2252

Open isaacl opened 4 years ago

isaacl commented 4 years ago

Probably doesn't matter most of the time, but arrays are poorly suited as a queue. shift() is an O(n) operation. Better would be a simple structure which has amortized O(1) operations:

class Queue {
  constructor() {
    this._pushQueue = [];
    this._removeQueue = []
  }

  pop() {
    if (this._removeQueue.length === 0 && this._pushQueue.length > 0) {
      this._removeQueue = this._pushQueue.reverse();
      this._pushQueue = [];
    }
    return this._removeQueue.pop();
  }

  push(...args) {
    this._pushQueue.push(...args)
  }

  length() {
    return this._pushQueue.length + this._removeQueue.length;
  }
}
charmander commented 4 years ago

Do you know if that implementation is published as a package anywhere?

I wonder how this compares to double-ended-queue for pg’s purposes. It’s probably a bit slower, but that’s minor compared to everything else pg does, plus it’s simpler and the arrays shrink automatically. A clear winner?

isaacl commented 4 years ago

I wrote that for this bug. Yes its is probably a bit slower than a double ended queue (or a circular buffer).