aboutsummaryrefslogtreecommitdiff
path: root/src/bun.js/builtins/js
diff options
context:
space:
mode:
authorGravatar Jarred Sumner <jarred@jarredsumner.com> 2023-01-04 03:30:15 -0800
committerGravatar GitHub <noreply@github.com> 2023-01-04 03:30:15 -0800
commit4a328609b96609dbeb8dc98e19aa2f52d2e5eaab (patch)
tree36d16a77ab44f3b324e3508b6a3e9f4daecf9df2 /src/bun.js/builtins/js
parent021331f154123f9fb39ac47d5c98f5a9e1095ea4 (diff)
downloadbun-4a328609b96609dbeb8dc98e19aa2f52d2e5eaab.tar.gz
bun-4a328609b96609dbeb8dc98e19aa2f52d2e5eaab.tar.zst
bun-4a328609b96609dbeb8dc98e19aa2f52d2e5eaab.zip
10x faster `new Buffer` (#1717)
Co-authored-by: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com>
Diffstat (limited to 'src/bun.js/builtins/js')
-rw-r--r--src/bun.js/builtins/js/JSBufferConstructor.js10
-rw-r--r--src/bun.js/builtins/js/JSBufferPrototype.js74
2 files changed, 46 insertions, 38 deletions
diff --git a/src/bun.js/builtins/js/JSBufferConstructor.js b/src/bun.js/builtins/js/JSBufferConstructor.js
index bfb122034..48342fe26 100644
--- a/src/bun.js/builtins/js/JSBufferConstructor.js
+++ b/src/bun.js/builtins/js/JSBufferConstructor.js
@@ -25,6 +25,14 @@
// ^ that comment is required or the builtins generator will have a fit.
+function alloc(n) {
+ "use strict";
+ if (typeof n !== "number" || n < 0) {
+ @throwRangeError("n must be a positive integer less than 2^32");
+ }
+
+ return new this(n);
+}
function from(items) {
"use strict";
@@ -64,5 +72,5 @@ function from(items) {
// Don't pass the second argument because Node's Buffer.from doesn't accept
// a function and Uint8Array.from requires it if it exists
// That means we cannot use @tailCallFowrardArguments here, sadly
- return this.toBuffer(@Uint8Array.from(arrayLike));
+ return new this(@Uint8Array.from(arrayLike).buffer);
}
diff --git a/src/bun.js/builtins/js/JSBufferPrototype.js b/src/bun.js/builtins/js/JSBufferPrototype.js
index 689dc2dde..ef3332d88 100644
--- a/src/bun.js/builtins/js/JSBufferPrototype.js
+++ b/src/bun.js/builtins/js/JSBufferPrototype.js
@@ -31,176 +31,176 @@
function setBigUint64(offset, value, le) {
"use strict";
- return this.dataView.setBigUint64(offset, value, le);
+ return (this.@dataView ||= new DataView(this.buffer, this.byteOffset, this.byteLength)).setBigUint64(offset, value, le);
}
function readInt8(offset) {
"use strict";
- return this.dataView.getInt8(offset);
+ return (this.@dataView ||= new DataView(this.buffer, this.byteOffset, this.byteLength)).getInt8(offset);
}
function readUInt8(offset) {
"use strict";
- return this.dataView.getUint8(offset);
+ return (this.@dataView ||= new DataView(this.buffer, this.byteOffset, this.byteLength)).getUint8(offset);
}
function readInt16LE(offset) {
"use strict";
- return this.dataView.getInt16(offset, true);
+ return (this.@dataView ||= new DataView(this.buffer, this.byteOffset, this.byteLength)).getInt16(offset, true);
}
function readInt16BE(offset) {
"use strict";
- return this.dataView.getInt16(offset, false);
+ return (this.@dataView ||= new DataView(this.buffer, this.byteOffset, this.byteLength)).getInt16(offset, false);
}
function readUInt16LE(offset) {
"use strict";
- return this.dataView.getUint16(offset, true);
+ return (this.@dataView ||= new DataView(this.buffer, this.byteOffset, this.byteLength)).getUint16(offset, true);
}
function readUInt16BE(offset) {
"use strict";
- return this.dataView.getUint16(offset, false);
+ return (this.@dataView ||= new DataView(this.buffer, this.byteOffset, this.byteLength)).getUint16(offset, false);
}
function readInt32LE(offset) {
"use strict";
- return this.dataView.getInt32(offset, true);
+ return (this.@dataView ||= new DataView(this.buffer, this.byteOffset, this.byteLength)).getInt32(offset, true);
}
function readInt32BE(offset) {
"use strict";
- return this.dataView.getInt32(offset, false);
+ return (this.@dataView ||= new DataView(this.buffer, this.byteOffset, this.byteLength)).getInt32(offset, false);
}
function readUInt32LE(offset) {
"use strict";
- return this.dataView.getUint32(offset, true);
+ return (this.@dataView ||= new DataView(this.buffer, this.byteOffset, this.byteLength)).getUint32(offset, true);
}
function readUInt32BE(offset) {
"use strict";
- return this.dataView.getUint32(offset, false);
+ return (this.@dataView ||= new DataView(this.buffer, this.byteOffset, this.byteLength)).getUint32(offset, false);
}
function readFloatLE(offset) {
"use strict";
- return this.dataView.getFloat32(offset, true);
+ return (this.@dataView ||= new DataView(this.buffer, this.byteOffset, this.byteLength)).getFloat32(offset, true);
}
function readFloatBE(offset) {
"use strict";
- return this.dataView.getFloat32(offset, false);
+ return (this.@dataView ||= new DataView(this.buffer, this.byteOffset, this.byteLength)).getFloat32(offset, false);
}
function readDoubleLE(offset) {
"use strict";
- return this.dataView.getFloat64(offset, true);
+ return (this.@dataView ||= new DataView(this.buffer, this.byteOffset, this.byteLength)).getFloat64(offset, true);
}
function readDoubleBE(offset) {
"use strict";
- return this.dataView.getFloat64(offset, false);
+ return (this.@dataView ||= new DataView(this.buffer, this.byteOffset, this.byteLength)).getFloat64(offset, false);
}
function readBigInt64LE(offset) {
"use strict";
- return this.dataView.getBigInt64(offset, true);
+ return (this.@dataView ||= new DataView(this.buffer, this.byteOffset, this.byteLength)).getBigInt64(offset, true);
}
function readBigInt64BE(offset) {
"use strict";
- return this.dataView.getBigInt64(offset, false);
+ return (this.@dataView ||= new DataView(this.buffer, this.byteOffset, this.byteLength)).getBigInt64(offset, false);
}
function readBigUInt64LE(offset) {
"use strict";
- return this.dataView.getBigUint64(offset, true);
+ return (this.@dataView ||= new DataView(this.buffer, this.byteOffset, this.byteLength)).getBigUint64(offset, true);
}
function readBigUInt64BE(offset) {
"use strict";
- return this.dataView.getBigUint64(offset, false);
+ return (this.@dataView ||= new DataView(this.buffer, this.byteOffset, this.byteLength)).getBigUint64(offset, false);
}
function writeInt8(value, offset) {
"use strict";
- this.dataView.setInt8(offset, value);
+ (this.@dataView ||= new DataView(this.buffer, this.byteOffset, this.byteLength)).setInt8(offset, value);
return offset + 1;
}
function writeUInt8(value, offset) {
"use strict";
- this.dataView.setUint8(offset, value);
+ (this.@dataView ||= new DataView(this.buffer, this.byteOffset, this.byteLength)).setUint8(offset, value);
return offset + 1;
}
function writeInt16LE(value, offset) {
"use strict";
- this.dataView.setInt16(offset, value, true);
+ (this.@dataView ||= new DataView(this.buffer, this.byteOffset, this.byteLength)).setInt16(offset, value, true);
return offset + 2;
}
function writeInt16BE(value, offset) {
"use strict";
- this.dataView.setInt16(offset, value, false);
+ (this.@dataView ||= new DataView(this.buffer, this.byteOffset, this.byteLength)).setInt16(offset, value, false);
return offset + 2;
}
function writeUInt16LE(value, offset) {
"use strict";
- this.dataView.setUint16(offset, value, true);
+ (this.@dataView ||= new DataView(this.buffer, this.byteOffset, this.byteLength)).setUint16(offset, value, true);
return offset + 2;
}
function writeUInt16BE(value, offset) {
"use strict";
- this.dataView.setUint16(offset, value, false);
+ (this.@dataView ||= new DataView(this.buffer, this.byteOffset, this.byteLength)).setUint16(offset, value, false);
return offset + 2;
}
function writeInt32LE(value, offset) {
"use strict";
- this.dataView.setInt32(offset, value, true);
+ (this.@dataView ||= new DataView(this.buffer, this.byteOffset, this.byteLength)).setInt32(offset, value, true);
return offset + 4;
}
function writeInt32BE(value, offset) {
"use strict";
- this.dataView.setInt32(offset, value, false);
+ (this.@dataView ||= new DataView(this.buffer, this.byteOffset, this.byteLength)).setInt32(offset, value, false);
return offset + 4;
}
function writeUInt32LE(value, offset) {
"use strict";
- this.dataView.setUint32(offset, value, true);
+ (this.@dataView ||= new DataView(this.buffer, this.byteOffset, this.byteLength)).setUint32(offset, value, true);
return offset + 4;
}
function writeUInt32BE(value, offset) {
"use strict";
- this.dataView.setUint32(offset, value, false);
+ (this.@dataView ||= new DataView(this.buffer, this.byteOffset, this.byteLength)).setUint32(offset, value, false);
return offset + 4;
}
function writeFloatLE(value, offset) {
"use strict";
- this.dataView.setFloat32(offset, value, true);
+ (this.@dataView ||= new DataView(this.buffer, this.byteOffset, this.byteLength)).setFloat32(offset, value, true);
return offset + 4;
}
function writeFloatBE(value, offset) {
"use strict";
- this.dataView.setFloat32(offset, value, false);
+ (this.@dataView ||= new DataView(this.buffer, this.byteOffset, this.byteLength)).setFloat32(offset, value, false);
return offset + 4;
}
function writeDoubleLE(value, offset) {
"use strict";
- this.dataView.setFloat64(offset, value, true);
+ (this.@dataView ||= new DataView(this.buffer, this.byteOffset, this.byteLength)).setFloat64(offset, value, true);
return offset + 8;
}
function writeDoubleBE(value, offset) {
"use strict";
- this.dataView.setFloat64(offset, value, false);
+ (this.@dataView ||= new DataView(this.buffer, this.byteOffset, this.byteLength)).setFloat64(offset, value, false);
return offset + 8;
}
function writeBigInt64LE(value, offset) {
"use strict";
- this.dataView.setBigInt64(offset, value, true);
+ (this.@dataView ||= new DataView(this.buffer, this.byteOffset, this.byteLength)).setBigInt64(offset, value, true);
return offset + 8;
}
function writeBigInt64BE(value, offset) {
"use strict";
- this.dataView.setBigInt64(offset, value, false);
+ (this.@dataView ||= new DataView(this.buffer, this.byteOffset, this.byteLength)).setBigInt64(offset, value, false);
return offset + 8;
}
function writeBigUInt64LE(value, offset) {
"use strict";
- this.dataView.setBigUint64(offset, value, true);
+ (this.@dataView ||= new DataView(this.buffer, this.byteOffset, this.byteLength)).setBigUint64(offset, value, true);
return offset + 8;
}
function writeBigUInt64BE(value, offset) {
"use strict";
- this.dataView.setBigUint64(offset, value, false);
+ (this.@dataView ||= new DataView(this.buffer, this.byteOffset, this.byteLength)).setBigUint64(offset, value, false);
return offset + 8;
}