Skip to content

Commit 80c4703

Browse files
committed
Optimize buffer write
1 parent 10eb39a commit 80c4703

File tree

1 file changed

+4
-7
lines changed

1 file changed

+4
-7
lines changed

JavaScript/3-ring.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,11 @@ class RingBuffer {
1111
const { size, offset } = this;
1212
const { length } = data;
1313
const available = size - offset;
14-
const end = Math.min(offset + length, size + 1);
14+
const len = Math.min(available, size, length);
1515
const rest = available - length;
16-
let position = 0;
17-
for (let i = offset; i < end; i++) {
18-
this.buffer[i] = data.charCodeAt(position++);
19-
}
20-
this.offset += position;
21-
if (this.offset > size) this.offset = 0;
16+
this.buffer.write(data, offset, len);
17+
this.offset += len;
18+
if (this.offset === size) this.offset = 0;
2219
if (rest < 0) this.write(data.slice(rest));
2320
}
2421
}

0 commit comments

Comments
 (0)