Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
streams: refactor BufferList into ES6 class
  • Loading branch information
jasnell committed Apr 27, 2017
commit 3d75f9f045393712198d511ecaf23b89eb08327f
116 changes: 58 additions & 58 deletions lib/internal/streams/BufferList.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,71 +2,71 @@

const Buffer = require('buffer').Buffer;

module.exports = BufferList;
module.exports = class BufferList {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you benchmark this change?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, negligible difference in master with 5.7. Notable improvement under TF-I ... tho I'd have to pull the numbers back up. It was a week or so ago when I ran it.

constructor() {
this.head = null;
this.tail = null;
this.length = 0;
}

function BufferList() {
this.head = null;
this.tail = null;
this.length = 0;
}
push(v) {
const entry = { data: v, next: null };
if (this.length > 0)
this.tail.next = entry;
else
this.head = entry;
this.tail = entry;
++this.length;
}

BufferList.prototype.push = function(v) {
const entry = { data: v, next: null };
if (this.length > 0)
this.tail.next = entry;
else
unshift(v) {
const entry = { data: v, next: this.head };
if (this.length === 0)
this.tail = entry;
this.head = entry;
this.tail = entry;
++this.length;
};
++this.length;
}

BufferList.prototype.unshift = function(v) {
const entry = { data: v, next: this.head };
if (this.length === 0)
this.tail = entry;
this.head = entry;
++this.length;
};
shift() {
if (this.length === 0)
return;
const ret = this.head.data;
if (this.length === 1)
this.head = this.tail = null;
else
this.head = this.head.next;
--this.length;
return ret;
}

BufferList.prototype.shift = function() {
if (this.length === 0)
return;
const ret = this.head.data;
if (this.length === 1)
clear() {
this.head = this.tail = null;
else
this.head = this.head.next;
--this.length;
return ret;
};

BufferList.prototype.clear = function() {
this.head = this.tail = null;
this.length = 0;
};
this.length = 0;
}

BufferList.prototype.join = function(s) {
if (this.length === 0)
return '';
var p = this.head;
var ret = '' + p.data;
while (p = p.next)
ret += s + p.data;
return ret;
};
join(s) {
if (this.length === 0)
return '';
var p = this.head;
var ret = '' + p.data;
while (p = p.next)
ret += s + p.data;
return ret;
}

BufferList.prototype.concat = function(n) {
if (this.length === 0)
return Buffer.alloc(0);
if (this.length === 1)
return this.head.data;
const ret = Buffer.allocUnsafe(n >>> 0);
var p = this.head;
var i = 0;
while (p) {
p.data.copy(ret, i);
i += p.data.length;
p = p.next;
concat(n) {
if (this.length === 0)
return Buffer.alloc(0);
if (this.length === 1)
return this.head.data;
const ret = Buffer.allocUnsafe(n >>> 0);
var p = this.head;
var i = 0;
while (p) {
p.data.copy(ret, i);
i += p.data.length;
p = p.next;
}
return ret;
}
return ret;
};