Skip to content

Commit 58296e4

Browse files
committed
buffer: Fixing deopt when constructed with strings
When a buffer was created using `new Buffer('A String')`, internally within the buffer class we would read arguments[1], however in this case we would be reading outside of the array causing a deopt to occur. This fixes this issue by casting the type to a string before use, and removing the usage of the arguments object.
1 parent 7b355c5 commit 58296e4

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

lib/buffer.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ function alignPool() {
4040
}
4141

4242

43-
function Buffer(arg) {
43+
function Buffer(arg, encoding) {
4444
// Common case.
4545
if (typeof arg === 'number') {
4646
// If less than zero, or NaN.
@@ -51,7 +51,11 @@ function Buffer(arg) {
5151

5252
// Slightly less common case.
5353
if (typeof arg === 'string') {
54-
return fromString(arg, arguments[1]);
54+
encoding = encoding || '';
55+
if (typeof encoding !== 'string' || encoding === '')
56+
encoding = 'utf8';
57+
58+
return fromString(arg, encoding);
5559
}
5660

5761
// Unusual.
@@ -103,9 +107,6 @@ function allocate(size) {
103107

104108

105109
function fromString(string, encoding) {
106-
if (typeof encoding !== 'string' || encoding === '')
107-
encoding = 'utf8';
108-
109110
var length = byteLength(string, encoding);
110111
if (length >= (Buffer.poolSize >>> 1))
111112
return binding.createFromString(string, encoding);

0 commit comments

Comments
 (0)