@@ -56,6 +56,9 @@ const {
5656
5757const encoder = new TextEncoder ( ) ;
5858
59+ // Cached resolved promise to avoid allocating a new one on every sync fast-path.
60+ const kResolvedPromise = PromiseResolve ( ) ;
61+
5962// Non-exported symbol for internal cancel notification from BroadcastImpl
6063// to BroadcastWriter. Because this symbol is not exported, external code
6164// cannot call it.
@@ -388,11 +391,37 @@ class BroadcastWriter {
388391 return this . _broadcast . _getDesiredSize ( ) ;
389392 }
390393
391- async write ( chunk , options ) {
394+ write ( chunk , options ) {
395+ // Fast path: no signal, writer open, buffer has space
396+ if ( ! options ?. signal && ! this . _closed && ! this . _aborted &&
397+ this . _broadcast . _canWrite ( ) ) {
398+ const converted =
399+ typeof chunk === 'string' ? encoder . encode ( chunk ) : chunk ;
400+ this . _broadcast . _write ( [ converted ] ) ;
401+ this . _totalBytes += converted . byteLength ;
402+ return kResolvedPromise ;
403+ }
392404 return this . writev ( [ chunk ] , options ) ;
393405 }
394406
395- async writev ( chunks , options ) {
407+ writev ( chunks , options ) {
408+ // Fast path: no signal, writer open, buffer has space
409+ if ( ! options ?. signal && ! this . _closed && ! this . _aborted &&
410+ this . _broadcast . _canWrite ( ) ) {
411+ const converted = allUint8Array ( chunks ) ?
412+ ArrayPrototypeSlice ( chunks ) :
413+ ArrayPrototypeMap ( chunks , ( c ) =>
414+ ( typeof c === 'string' ? encoder . encode ( c ) : c ) ) ;
415+ this . _broadcast . _write ( converted ) ;
416+ for ( let i = 0 ; i < converted . length ; i ++ ) {
417+ this . _totalBytes += converted [ i ] . byteLength ;
418+ }
419+ return kResolvedPromise ;
420+ }
421+ return this . _writevSlow ( chunks , options ) ;
422+ }
423+
424+ async _writevSlow ( chunks , options ) {
396425 const signal = options ?. signal ;
397426
398427 // Check for pre-aborted signal
@@ -404,9 +433,9 @@ class BroadcastWriter {
404433 throw new ERR_INVALID_STATE ( 'Writer is closed' ) ;
405434 }
406435
407- const converted = allUint8Array ( chunks )
408- ? ArrayPrototypeSlice ( chunks )
409- : ArrayPrototypeMap ( chunks , ( c ) =>
436+ const converted = allUint8Array ( chunks ) ?
437+ ArrayPrototypeSlice ( chunks ) :
438+ ArrayPrototypeMap ( chunks , ( c ) =>
410439 ( typeof c === 'string' ? encoder . encode ( c ) : c ) ) ;
411440
412441 if ( this . _broadcast . _write ( converted ) ) {
@@ -447,9 +476,9 @@ class BroadcastWriter {
447476 writevSync ( chunks ) {
448477 if ( this . _closed || this . _aborted ) return false ;
449478 if ( ! this . _broadcast . _canWrite ( ) ) return false ;
450- const converted = allUint8Array ( chunks )
451- ? ArrayPrototypeSlice ( chunks )
452- : ArrayPrototypeMap ( chunks , ( c ) =>
479+ const converted = allUint8Array ( chunks ) ?
480+ ArrayPrototypeSlice ( chunks ) :
481+ ArrayPrototypeMap ( chunks , ( c ) =>
453482 ( typeof c === 'string' ? encoder . encode ( c ) : c ) ) ;
454483 if ( this . _broadcast . _write ( converted ) ) {
455484 for ( let i = 0 ; i < converted . length ; i ++ ) {
@@ -461,12 +490,12 @@ class BroadcastWriter {
461490 }
462491
463492 // end() is synchronous internally - signal accepted for interface compliance.
464- async end ( options ) {
465- if ( this . _closed ) return this . _totalBytes ;
493+ end ( options ) {
494+ if ( this . _closed ) return PromiseResolve ( this . _totalBytes ) ;
466495 this . _closed = true ;
467496 this . _broadcast . _end ( ) ;
468497 this . _resolvePendingDrains ( false ) ;
469- return this . _totalBytes ;
498+ return PromiseResolve ( this . _totalBytes ) ;
470499 }
471500
472501 endSync ( ) {
@@ -477,14 +506,15 @@ class BroadcastWriter {
477506 return this . _totalBytes ;
478507 }
479508
480- async fail ( reason ) {
481- if ( this . _aborted ) return ;
509+ fail ( reason ) {
510+ if ( this . _aborted ) return kResolvedPromise ;
482511 this . _aborted = true ;
483512 this . _closed = true ;
484513 const error = reason ?? new ERR_INVALID_STATE ( 'Failed' ) ;
485514 this . _rejectPendingWrites ( error ) ;
486515 this . _rejectPendingDrains ( error ) ;
487516 this . _broadcast . _abort ( error ) ;
517+ return kResolvedPromise ;
488518 }
489519
490520 failSync ( reason ) {
0 commit comments