Created
May 7, 2024 03:03
-
-
Save bytemain/69fc86bc36553adfad1be50967e99a64 to your computer and use it in GitHub Desktop.
Buffer to JSON playground
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import filesize from 'filesize' | |
import crypto from 'crypto' | |
function testIt(buffer: Buffer) { | |
console.log("========") | |
console.log(`原始 buffer 长度`, filesize.filesize(buffer.byteLength)); | |
const stringified = JSON.stringify(buffer) | |
if (stringified.length < 200) { | |
console.log(`原始数据`, buffer); | |
console.log(`序列化结果:`, stringified); | |
} | |
const buffer2 = Buffer.from(stringified) | |
console.log(`序列化后要传输的二进制长度:`, filesize.filesize(buffer2.byteLength)); | |
} | |
testIt(Buffer.from("hello-world,你好世界")) | |
// 100b | |
testIt(crypto.randomBytes(100)) | |
// 10k | |
testIt(crypto.randomBytes(10 * 1024)) | |
// 1m | |
testIt(crypto.randomBytes(1024 * 1024)) | |
// 10m | |
testIt(crypto.randomBytes(10 * 1024 * 1024)) | |
// 100m | |
testIt(crypto.randomBytes(100 * 1024 * 1024)) | |
// 200m | |
testIt(crypto.randomBytes(200 * 1024 * 1024)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment