Skip to content

Commit 608ec41

Browse files
author
Jérôme Benoit
authored
perf(fixed-deque): avoid costly modulo op in hot code path
Signed-off-by: Jérôme Benoit <[email protected]>
1 parent 12a5d91 commit 608ec41

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

fixed-deque.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ FixedDeque.prototype.push = function(item) {
4848
if (this.size === this.capacity)
4949
throw new Error('mnemonist/fixed-deque.push: deque capacity (' + this.capacity + ') exceeded!');
5050

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

5355
this.items[index] = item;
5456

@@ -85,7 +87,9 @@ FixedDeque.prototype.pop = function() {
8587
if (this.size === 0)
8688
return;
8789

88-
const index = (this.start + this.size - 1) % this.capacity;
90+
var index = this.start + this.size - 1;
91+
if (index > this.capacity)
92+
index -= this.capacity;
8993

9094
this.size--;
9195

0 commit comments

Comments
 (0)