Skip to content

Commit

Permalink
perf(fixed-deque): avoid costly modulo op in hot code path
Browse files Browse the repository at this point in the history
Signed-off-by: Jérôme Benoit <[email protected]>
  • Loading branch information
Jérôme Benoit authored Feb 2, 2024
1 parent 12a5d91 commit 608ec41
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions fixed-deque.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ FixedDeque.prototype.push = function(item) {
if (this.size === this.capacity)
throw new Error('mnemonist/fixed-deque.push: deque capacity (' + this.capacity + ') exceeded!');

var index = (this.start + this.size) % this.capacity;
var index = this.start + this.size;
if (index >= this.capacity)
index -= this.capacity;

this.items[index] = item;

Expand Down Expand Up @@ -85,7 +87,9 @@ FixedDeque.prototype.pop = function() {
if (this.size === 0)
return;

const index = (this.start + this.size - 1) % this.capacity;
var index = this.start + this.size - 1;
if (index > this.capacity)
index -= this.capacity;

this.size--;

Expand Down

0 comments on commit 608ec41

Please sign in to comment.