forked from oven-sh/bun
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuffer.js
136 lines (130 loc) · 6.13 KB
/
buffer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// import { Buffer } from "buffer";
var buf = new Buffer(1024);
var view = new DataView(buf.buffer);
var INTERVAL = 9999999;
var time = (name, fn) => {
for (let i = 0; i < INTERVAL; i++) fn();
console.time(name.padEnd('Buffer.write(string, "latin1")'.length));
for (let i = 0; i < INTERVAL; i++) fn();
console.timeEnd(name.padEnd('Buffer.write(string, "latin1")'.length));
};
console.log(
`Run ${new Intl.NumberFormat().format(INTERVAL)} times with a warmup:`,
"\n"
);
const stringToWrite = "hellooooohellooooo";
time('Buffer.write(string, "utf8")', () => buf.write(stringToWrite, "utf8"));
time('Buffer.write(string, "ascii")', () => buf.write(stringToWrite, "ascii"));
time('Buffer.write(string, "latin1")', () =>
buf.write(stringToWrite, "latin1")
);
time("Buffer.readBigInt64BE ", () => buf.readBigInt64BE(0));
// time("DataView.getBigInt64 (BE)", () => view.getBigInt64(0, false));
// console.log("");
time("Buffer.readBigInt64LE ", () => buf.readBigInt64LE(0));
// time("DataView.readBigInt64 (LE)", () => view.getBigInt64(0, true));
// console.log("");
time("Buffer.readBigUInt64BE ", () => buf.readBigUInt64BE(0));
// time("DataView.getBigUInt64 (BE)", () => view.getBigUint64(0, false));
// console.log("");
time("Buffer.readBigUInt64LE ", () => buf.readBigUInt64LE(0));
// time("DataView.readBigUInt64 (LE)", () => view.getBigUint64(0, true));
// console.log("");
time("Buffer.readDoubleBE ", () => buf.readDoubleBE(0));
// time("DataView.getDouble (BE)", () => view.getFloat64(0, false));
// console.log("");
time("Buffer.readDoubleLE ", () => buf.readDoubleLE(0));
// time("DataView.readDouble (LE)", () => view.getFloat64(0, true));
// console.log("");
time("Buffer.readFloatBE ", () => buf.readFloatBE(0));
// time("DataView.getFloat (BE)", () => view.getFloat32(0, false));
// console.log("");
time("Buffer.readFloatLE ", () => buf.readFloatLE(0));
// time("DataView.readFloat (LE)", () => view.getFloat32(0, true));
// console.log("");
time("Buffer.readInt16BE ", () => buf.readInt16BE(0));
// time("DataView.getInt16 (BE)", () => view.getInt16(0, false));
// console.log("");
time("Buffer.readInt16LE ", () => buf.readInt16LE(0));
// time("DataView.readInt16 (LE)", () => view.getInt16(0, true));
// console.log("");
time("Buffer.readInt32BE ", () => buf.readInt32BE(0));
// time("DataView.getInt32 (BE)", () => view.getInt32(0, false));
// console.log("");
time("Buffer.readInt32LE ", () => buf.readInt32LE(0));
// time("DataView.readInt32 (LE)", () => view.getInt32(0, true));
// console.log("");
time("Buffer.readInt8 ", () => buf.readInt8(0));
// time("DataView.readInt (t8)", () => view.getInt8(0));
// console.log("");
time("Buffer.readUInt16BE ", () => buf.readUInt16BE(0));
// time("DataView.getUInt16 (BE)", () => view.getUint16(0, false));
// console.log("");
time("Buffer.readUInt16LE ", () => buf.readUInt16LE(0));
// time("DataView.readUInt16 (LE)", () => view.getUint16(0, true));
// console.log("");
time("Buffer.readUInt32BE ", () => buf.readUInt32BE(0));
// time("DataView.getUInt32 (BE)", () => view.getUint32(0, false));
// console.log("");
time("Buffer.readUInt32LE ", () => buf.readUInt32LE(0));
// time("DataView.getUInt32 (LE)", () => view.getUint32(0, true));
// console.log("");
time("Buffer.readUInt8 ", () => buf.readUInt8(0));
// time("DataView.getUInt (t8)", () => view.getUint8(0));
// console.log("");
time("Buffer.writeBigInt64BE", () => buf.writeBigInt64BE(BigInt(0), 0));
// time("DataView.getBigInt64 (BE)", () => view.getBigInt64(0, false));
// console.log("");
time("Buffer.writeBigInt64LE", () => buf.writeBigInt64LE(BigInt(0), 0));
// time("DataView.readBigInt64 (LE)", () => view.getBigInt64(0, true));
// console.log("");
time("Buffer.writeBigUInt64BE", () => buf.writeBigUInt64BE(BigInt(0), 0));
// time("DataView.getBigUInt64 (BE)", () => view.getBigUint64(0, false));
// console.log("");
time("Buffer.writeBigUInt64LE", () => buf.writeBigUInt64LE(BigInt(0), 0));
// time("DataView.readBigUInt64 (LE)", () => view.getBigUint64(0, true));
// console.log("");
time("Buffer.writeDoubleBE ", () => buf.writeDoubleBE(0, 0));
// time("DataView.getDouble (BE)", () => view.getFloat64(0, false));
// console.log("");
time("Buffer.writeDoubleLE ", () => buf.writeDoubleLE(0, 0));
// time("DataView.readDouble (LE)", () => view.getFloat64(0, true));
// console.log("");
time("Buffer.writeFloatBE ", () => buf.writeFloatBE(0, 0));
// time("DataView.getFloat (BE)", () => view.getFloat32(0, false));
// console.log("");
time("Buffer.writeFloatLE ", () => buf.writeFloatLE(0, 0));
// time("DataView.readFloat (LE)", () => view.getFloat32(0, true));
// console.log("");
time("Buffer.writeInt16BE ", () => buf.writeInt16BE(0, 0));
// time("DataView.getInt16 (BE)", () => view.getInt16(0, false));
// console.log("");
time("Buffer.writeInt16LE ", () => buf.writeInt16LE(0, 0));
// time("DataView.readInt16 (LE)", () => view.getInt16(0, true));
// console.log("");
time("Buffer.writeInt32BE ", () => buf.writeInt32BE(0, 0));
// time("DataView.getInt32 (BE)", () => view.getInt32(0, false));
// console.log("");
time("Buffer.writeInt32LE ", () => buf.writeInt32LE(0, 0));
// time("DataView.readInt32 (LE)", () => view.getInt32(0, true));
// console.log("");
time("Buffer.writeInt8 ", () => buf.writeInt8(0, 0));
// time("DataView.readInt (t8)", () => view.getInt8(0));
// console.log("");
time("Buffer.writeUInt16BE ", () => buf.writeUInt16BE(0, 0));
// time("DataView.getUInt16 (BE)", () => view.getUint16(0, false));
// console.log("");
time("Buffer.writeUInt16LE ", () => buf.writeUInt16LE(0, 0));
// time("DataView.readUInt16 (LE)", () => view.getUint16(0, true));
// console.log("");
time("Buffer.writeUInt32BE ", () => buf.writeUInt32BE(0, 0));
// time("DataView.getUInt32 (BE)", () => view.getUint32(0, false));
// console.log("");
time("Buffer.writeUInt32LE ", () => buf.writeUInt32LE(0, 0));
// time("DataView.getUInt32 (LE)", () => view.getUint32(0, true));
// console.log("");
time("Buffer.writeUInt8 ", () => buf.writeUInt8(0, 0));
// time("DataView.getUInt (t8)", () => view.getUint8(0));
// console.log("");
buf.writeUInt8(10, 10);
console.assert(buf.readUInt8(10, 10) === 10);