MatthewWid / better-sse

⬆ Dead simple, dependency-less, spec-compliant server-sent events implementation for Node, written in TypeScript.
https://matthewwid.github.io/better-sse/
MIT License
558 stars 14 forks source link

Faster random event ID generation #33

Closed MatthewWid closed 2 years ago

MatthewWid commented 2 years ago

Resolves #30.

This MR updates the internal event ID generation to use the new randomUUID Node crypto-library function and falls back to the old method of using randomBytes if it is not available. As such, it also removes the guarantee of the format of the event ID from the API documentation.

This offers a significant performance boost when using the Session#push and Channel#broadcast methods.

Note that different to #30's recommendations, the event ID generation will now fallback to the old method, avoiding breaking backwards compatibility with Node 12, and as such does not require allowing the user to override the event ID generation function themselves.


Benchmarks before:

Benchmarking "Push events with channels.".
better-sse x 154,140 ops/sec ±4.59% (81 runs sampled)
sse-channel x 147,619 ops/sec ±12.56% (86 runs sampled)

Benchmarking "Push events with channels.".
better-sse x 151,673 ops/sec ±4.60% (80 runs sampled)
sse-channel x 156,103 ops/sec ±1.75% (89 runs sampled)

Benchmarking "Push events with channels.".
better-sse x 156,255 ops/sec ±4.56% (78 runs sampled)
sse-channel x 144,004 ops/sec ±12.88% (85 runs sampled)

Benchmarks after:

Benchmarking "Push events with channels.".
better-sse x 218,704 ops/sec ±10.83% (68 runs sampled)
sse-channel x 127,889 ops/sec ±21.00% (83 runs sampled)

Benchmarking "Push events with channels.".
better-sse x 242,813 ops/sec ±9.28% (75 runs sampled)
sse-channel x 116,833 ops/sec ±20.04% (76 runs sampled)

Benchmarking "Push events with channels.".
better-sse x 214,748 ops/sec ±10.64% (72 runs sampled)
sse-channel x 144,027 ops/sec ±1.30% (93 runs sampled)