@@ -150,11 +150,12 @@ const constants = ObjectDefineProperties({}, {
150150} ) ;
151151
152152Buffer . poolSize = 8 * 1024 ;
153- let poolSize , poolOffset , allocPool ;
153+ let poolSize , poolOffset , allocPool , allocBuffer ;
154154
155155function createPool ( ) {
156156 poolSize = Buffer . poolSize ;
157- allocPool = createUnsafeBuffer ( poolSize ) . buffer ;
157+ allocBuffer = createUnsafeBuffer ( poolSize ) ;
158+ allocPool = allocBuffer . buffer ;
158159 markAsUntransferable ( allocPool ) ;
159160 poolOffset = 0 ;
160161}
@@ -442,27 +443,33 @@ function allocate(size) {
442443}
443444
444445function fromStringFast ( string , ops ) {
445- const length = ops . byteLength ( string ) ;
446+ let length = string . length ;
447+
448+ if ( length > Buffer . poolSize )
449+ return createFromString ( string , ops . encodingVal ) ;
450+
451+ length <<= 2 ; // max utf8 byte length
452+
453+ if ( length >= ( Buffer . poolSize >>> 1 ) )
454+ length = ops . byteLength ( string ) ;
446455
447456 if ( length >= ( Buffer . poolSize >>> 1 ) )
448457 return createFromString ( string , ops . encodingVal ) ;
449458
450459 if ( length > ( poolSize - poolOffset ) )
451460 createPool ( ) ;
452- let b = new FastBuffer ( allocPool , poolOffset , length ) ;
453- const actual = ops . write ( b , string , 0 , length ) ;
454- if ( actual !== length ) {
455- // byteLength() may overestimate. That's a rare case, though.
456- b = new FastBuffer ( allocPool , poolOffset , actual ) ;
457- }
461+
462+ const actual = ops . write ( allocBuffer , string , poolOffset , length ) ;
463+ const b = new FastBuffer ( allocPool , poolOffset , actual ) ;
464+
458465 poolOffset += actual ;
459466 alignPool ( ) ;
460467 return b ;
461468}
462469
463470function fromString ( string , encoding ) {
464471 let ops ;
465- if ( typeof encoding !== 'string ' || encoding . length === 0 ) {
472+ if ( ! encoding || encoding === 'utf8 ' || typeof encoding !== 'string' ) {
466473 if ( string . length === 0 )
467474 return new FastBuffer ( ) ;
468475 ops = encodingOps . utf8 ;
0 commit comments