-
Notifications
You must be signed in to change notification settings - Fork 502
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
websocket: 200x faster generate mask (#3204)
* websocket: improve performance of generate mask * apply comments * Apply suggestions from code review * Apply suggestions from code review * Apply suggestions from code review Co-authored-by: Aras Abbasi <aras.abbasi@googlemail.com> * Apply suggestions from code review * Update lib/web/websocket/frame.js * Update lib/web/websocket/frame.js * Update lib/web/websocket/frame.js Co-authored-by: Aras Abbasi <aras.abbasi@googlemail.com> --------- Co-authored-by: Aras Abbasi <aras.abbasi@googlemail.com>
- Loading branch information
Showing
2 changed files
with
52 additions
and
8 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { randomFillSync, randomBytes } from 'node:crypto' | ||
import { bench, group, run } from 'mitata' | ||
|
||
const BUFFER_SIZE = 16384 | ||
|
||
const buf = Buffer.allocUnsafe(BUFFER_SIZE) | ||
let bufIdx = BUFFER_SIZE | ||
|
||
function generateMask () { | ||
if (bufIdx === BUFFER_SIZE) { | ||
bufIdx = 0 | ||
randomFillSync(buf, 0, BUFFER_SIZE) | ||
} | ||
return [buf[bufIdx++], buf[bufIdx++], buf[bufIdx++], buf[bufIdx++]] | ||
} | ||
|
||
group('generate', () => { | ||
bench('generateMask', () => generateMask()) | ||
bench('crypto.randomBytes(4)', () => randomBytes(4)) | ||
}) | ||
|
||
await run() |
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
Maybe faster to use a Int32Array view and apply mask 4 bytes at a time instead of 1?