@@ -934,43 +934,66 @@ Buffer.prototype.writeDoubleBE = function writeDoubleBE(val, offset, noAssert) {
934934// ES6 iterator
935935
936936var ITERATOR_KIND_KEYS = 1 ;
937- var ITERATOR_KIND_VALUES = 2 ;
938937var ITERATOR_KIND_ENTRIES = 3 ;
939938
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+
940951function BufferIterator ( buffer , kind ) {
941952 this . _buffer = buffer ;
942953 this . _kind = kind ;
943954 this . _index = 0 ;
944955}
945956
946- function BufferIteratorResult ( value , done ) {
947- this . value = value ;
948- this . done = done ;
949- }
950-
951957BufferIterator . prototype . next = function ( ) {
952958 var buffer = this . _buffer ;
953959 var kind = this . _kind ;
954960 var index = this . _index ;
955961
956962 if ( index >= buffer . length )
957- return new BufferIteratorResult ( undefined , true ) ;
963+ return finalResult ;
958964
959965 this . _index ++ ;
960966
961- if ( kind === ITERATOR_KIND_VALUES )
962- return new BufferIteratorResult ( buffer [ index ] , false ) ;
963-
964967 if ( kind === ITERATOR_KIND_ENTRIES )
965968 return new BufferIteratorResult ( [ index , buffer [ index ] ] , false ) ;
966969
967970 return new BufferIteratorResult ( index , false ) ;
968971} ;
969972
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+
970990BufferIterator . prototype [ Symbol . iterator ] = function ( ) {
971991 return this ;
972992} ;
973993
994+ BufferValueIterator . prototype [ Symbol . iterator ] =
995+ BufferIterator . prototype [ Symbol . iterator ] ;
996+
974997Buffer . prototype . keys = function ( ) {
975998 return new BufferIterator ( this , ITERATOR_KIND_KEYS ) ;
976999} ;
@@ -980,7 +1003,7 @@ Buffer.prototype.entries = function() {
9801003} ;
9811004
9821005Buffer . prototype . values = function ( ) {
983- return new BufferIterator ( this , ITERATOR_KIND_VALUES ) ;
1006+ return new BufferValueIterator ( this ) ;
9841007} ;
9851008
9861009Buffer . prototype [ Symbol . iterator ] = Buffer . prototype . values ;
0 commit comments