@@ -934,43 +934,66 @@ Buffer.prototype.writeDoubleBE = function writeDoubleBE(val, offset, noAssert) {
934
934
// ES6 iterator
935
935
936
936
var ITERATOR_KIND_KEYS = 1 ;
937
- var ITERATOR_KIND_VALUES = 2 ;
938
937
var ITERATOR_KIND_ENTRIES = 3 ;
939
938
939
+ function BufferIteratorResult ( value , done ) {
940
+ this . value = value ;
941
+ this . done = done ;
942
+ }
943
+
944
+ var resultCache = new Array ( 256 ) ;
945
+
946
+ for ( var i = 0 ; i < 256 ; i ++ )
947
+ resultCache [ i ] = Object . freeze ( new BufferIteratorResult ( i , false ) ) ;
948
+
949
+ var finalResult = Object . freeze ( new BufferIteratorResult ( undefined , true ) ) ;
950
+
940
951
function BufferIterator ( buffer , kind ) {
941
952
this . _buffer = buffer ;
942
953
this . _kind = kind ;
943
954
this . _index = 0 ;
944
955
}
945
956
946
- function BufferIteratorResult ( value , done ) {
947
- this . value = value ;
948
- this . done = done ;
949
- }
950
-
951
957
BufferIterator . prototype . next = function ( ) {
952
958
var buffer = this . _buffer ;
953
959
var kind = this . _kind ;
954
960
var index = this . _index ;
955
961
956
962
if ( index >= buffer . length )
957
- return new BufferIteratorResult ( undefined , true ) ;
963
+ return finalResult ;
958
964
959
965
this . _index ++ ;
960
966
961
- if ( kind === ITERATOR_KIND_VALUES )
962
- return new BufferIteratorResult ( buffer [ index ] , false ) ;
963
-
964
967
if ( kind === ITERATOR_KIND_ENTRIES )
965
968
return new BufferIteratorResult ( [ index , buffer [ index ] ] , false ) ;
966
969
967
970
return new BufferIteratorResult ( index , false ) ;
968
971
} ;
969
972
973
+ function BufferValueIterator ( buffer ) {
974
+ BufferIterator . call ( this , buffer , null ) ;
975
+ }
976
+
977
+ BufferValueIterator . prototype . next = function ( ) {
978
+ var buffer = this . _buffer ;
979
+ var index = this . _index ;
980
+
981
+ if ( index >= buffer . length )
982
+ return finalResult ;
983
+
984
+ this . _index ++ ;
985
+
986
+ return resultCache [ buffer [ index ] ] ;
987
+ } ;
988
+
989
+
970
990
BufferIterator . prototype [ Symbol . iterator ] = function ( ) {
971
991
return this ;
972
992
} ;
973
993
994
+ BufferValueIterator . prototype [ Symbol . iterator ] =
995
+ BufferIterator . prototype [ Symbol . iterator ] ;
996
+
974
997
Buffer . prototype . keys = function ( ) {
975
998
return new BufferIterator ( this , ITERATOR_KIND_KEYS ) ;
976
999
} ;
@@ -980,7 +1003,7 @@ Buffer.prototype.entries = function() {
980
1003
} ;
981
1004
982
1005
Buffer . prototype . values = function ( ) {
983
- return new BufferIterator ( this , ITERATOR_KIND_VALUES ) ;
1006
+ return new BufferValueIterator ( this ) ;
984
1007
} ;
985
1008
986
1009
Buffer . prototype [ Symbol . iterator ] = Buffer . prototype . values ;
0 commit comments