@@ -622,16 +622,6 @@ function base64Slice (buf, start, end) {
622
622
}
623
623
}
624
624
625
- function decodeCodePointsArray ( buf ) {
626
- var len = buf . length
627
-
628
- if ( len <= 0x10000 ) {
629
- return String . fromCharCode . apply ( String , buf ) // avoid extra slice()
630
- }
631
-
632
- return binarySlice ( buf , 0 , len )
633
- }
634
-
635
625
function utf8Slice ( buf , start , end ) {
636
626
end = Math . min ( buf . length , end )
637
627
var res = [ ]
@@ -700,7 +690,7 @@ function utf8Slice (buf, start, end) {
700
690
i += bytesPerSequence
701
691
}
702
692
703
- return decodeCodePointsArray ( res )
693
+ return binarySlice ( res , 0 , res . length )
704
694
}
705
695
706
696
function asciiSlice ( buf , start , end ) {
@@ -713,17 +703,28 @@ function asciiSlice (buf, start, end) {
713
703
return ret
714
704
}
715
705
706
+ // Based on http://stackoverflow.com/a/22747272/680742, the browser with
707
+ // the lowest limit is Chrome, with 0x10000 args.
708
+ var MAX_ARGUMENTS_LENGTH = 0x10000
709
+
716
710
function binarySlice ( buf , start , end ) {
717
- var chunk = 0x10000
711
+ var len = buf . length
712
+ end = Math . min ( len , end )
713
+
714
+ // TODO: verify, this is probably the average case
715
+ if ( start === 0 && end === len && end <= MAX_ARGUMENTS_LENGTH ) {
716
+ return String . fromCharCode . apply ( String , buf )
717
+ }
718
+
718
719
var res = ''
719
720
720
721
// Decode in chunks to avoid "call stack size exceeded".
721
- // Based on http://stackoverflow.com/a/22747272/680742, the browser with
722
- // the lowest limit is Chrome, with 0x10000 args.
723
- for ( var i = start ; i < end ; i += chunk ) {
724
- var chunkEnd = Math . min ( end , i + chunk )
722
+ for ( var i = start ; i < end ; i += MAX_ARGUMENTS_LENGTH ) {
723
+ var chunkEnd = Math . min ( i + MAX_ARGUMENTS_LENGTH , end )
724
+
725
725
res += String . fromCharCode . apply ( String , buf . slice ( i , chunkEnd ) )
726
726
}
727
+
727
728
return res
728
729
}
729
730
0 commit comments