Created
December 5, 2023 19:12
-
-
Save dhei/6dcda0d9f6178c0a312535465eb80ded to your computer and use it in GitHub Desktop.
Compare latency of JSONStream.parse and JSON.parse
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
#!/usr/bin/env node | |
const https = require('node:https') | |
const JSONStream = require('JSONStream') | |
const JSONStreamWithDestroyTest = async () => { | |
const r = [] | |
for (let i = 0; i < 10; i++) { | |
r.push(new Promise((resolve, reject) => { | |
const start = Date.now() | |
const req = https.request(new URL('https://registry.npmjs.org/npm'), function (res) { | |
res | |
.pipe(JSONStream.parse('dist-tags')) | |
.on('data', function (distTags) { | |
console.log('version: ' + distTags.latest + ', latency: ' + (Date.now() - start)) | |
resolve(distTags) | |
res.destroy() | |
}) | |
}) | |
req.on('error', (err) => { | |
reject(err) | |
}) | |
req.end() | |
})) | |
} | |
await Promise.all(r) | |
} | |
const JSONParseTest = async () => { | |
const r = [] | |
for (let i = 0; i < 10; i++) { | |
r.push(new Promise((resolve, reject) => { | |
const start = Date.now() | |
let raw = '' | |
const req = https.request(new URL('https://registry.npmjs.org/npm'), function (res) { | |
res.on('data', d => { | |
raw = raw + d.toString('utf8') | |
}) | |
res.on('end', () => { | |
const p = JSON.parse(raw) | |
console.log('version: ' + p['dist-tags'].latest + ', latency: ' + (Date.now() - start)) | |
resolve(p['dist-tags']) | |
}) | |
}) | |
req.on('error', (err) => { | |
reject(err) | |
}) | |
req.end() | |
})) | |
} | |
await Promise.all(r) | |
} | |
JSONStreamWithDestroyTest() | |
.catch(err => { | |
console.error(err) | |
process.exit(1) | |
}) | |
JSONParseTest() | |
.catch(err => { | |
console.error(err) | |
process.exit(1) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment