We deserve a better streams API for JavaScript
5 hours ago
- #javascript
- #streaming
- #performance
- The WHATWG Streams Standard (Web streams) was designed to provide a common API for handling data streams across browsers and servers.
- Web streams have fundamental usability and performance issues due to design decisions made a decade ago, which don't align with modern JavaScript practices.
- An alternative approach to streaming is proposed, leveraging JavaScript language primitives like async iteration, which can perform 2x to 120x faster than Web streams.
- Key issues with Web streams include excessive ceremony for common operations, a complex locking model, BYOB (bring your own buffer) reads that are rarely beneficial, and problematic backpressure mechanisms.
- The alternative approach simplifies stream creation and consumption, eliminates unnecessary locking, and provides explicit backpressure policies.
- Pull semantics are introduced, ensuring transforms execute only when data is being consumed, which improves efficiency and reduces memory usage.
- The new API supports both synchronous and asynchronous processing, allowing for zero-overhead synchronous pipelines when all components are synchronous.
- Multi-consumer patterns are made explicit, avoiding the hidden buffering issues of Web streams' tee() method.
- Benchmarks show significant performance improvements with the alternative approach, particularly in scenarios involving chained transforms and high-frequency small chunks.
- The proposal aims to start a conversation about improving streaming APIs, addressing real-world pain points experienced with Web streams.