File tree 2 files changed +21
-10
lines changed
2 files changed +21
-10
lines changed Original file line number Diff line number Diff line change @@ -4,16 +4,16 @@ const common = require('../common.js');
4
4
const assert = require ( 'assert' ) ;
5
5
const bench = common . createBenchmark ( main , {
6
6
source : [
7
- 'array' ,
8
- 'arraybuffer' ,
9
- 'arraybuffer-middle' ,
10
- 'buffer' ,
7
+ // 'array',
8
+ // 'arraybuffer',
9
+ // 'arraybuffer-middle',
10
+ // 'buffer',
11
11
'string' ,
12
12
'string-utf8' ,
13
13
'string-base64' ,
14
- 'object' ,
15
- 'uint8array' ,
16
- 'uint16array' ,
14
+ // 'object',
15
+ // 'uint8array',
16
+ // 'uint16array',
17
17
] ,
18
18
len : [ 100 , 2048 ] ,
19
19
n : [ 8e5 ] ,
Original file line number Diff line number Diff line change @@ -442,21 +442,32 @@ function allocate(size) {
442
442
}
443
443
444
444
function fromStringFast ( string , ops ) {
445
- const length = ops . byteLength ( string ) ;
445
+ const poolLength = Buffer . poolSize >>> 1 ;
446
+ const stringLength = string . length
446
447
447
- if ( length >= ( Buffer . poolSize >>> 1 ) )
448
+ if ( stringLength > poolLength )
449
+ return createFromString ( string , ops . encodingVal ) ;
450
+
451
+ let length = stringLength * 4 ; // max utf8 byte length
452
+
453
+ if ( length >= poolLength )
454
+ length = ops . byteLength ( string ) ;
455
+
456
+ if ( length >= poolLength )
448
457
return createFromString ( string , ops . encodingVal ) ;
449
458
450
459
if ( length > ( poolSize - poolOffset ) )
451
460
createPool ( ) ;
461
+
452
462
let b = new FastBuffer ( allocPool , poolOffset , length ) ;
453
463
const actual = ops . write ( b , string , 0 , length ) ;
454
464
if ( actual !== length ) {
455
- // byteLength() may overestimate. That's a rare case, though.
456
465
b = new FastBuffer ( allocPool , poolOffset , actual ) ;
457
466
}
467
+
458
468
poolOffset += actual ;
459
469
alignPool ( ) ;
470
+
460
471
return b ;
461
472
}
462
473
You can’t perform that action at this time.
0 commit comments