File tree Expand file tree Collapse file tree 2 files changed +9
-15
lines changed Expand file tree Collapse file tree 2 files changed +9
-15
lines changed Original file line number Diff line number Diff line change @@ -5,6 +5,7 @@ import {randomBytes} from 'node:crypto';
55import benchmark from 'benchmark' ;
66import {
77 base64ToString ,
8+ compareUint8Arrays ,
89 concatUint8Arrays ,
910 hexToUint8Array ,
1011 isUint8Array ,
@@ -17,6 +18,8 @@ import {
1718
1819const oneMb = 1024 * 1024 ;
1920const largeUint8Array = new Uint8Array ( randomBytes ( oneMb ) . buffer ) ;
21+ // eslint-disable-next-line unicorn/prefer-spread
22+ const largeUint8ArrayDuplicate = largeUint8Array . slice ( ) ;
2023const textFromUint8Array = uint8ArrayToString ( largeUint8Array ) ;
2124const base64FromUint8Array = Buffer . from ( textFromUint8Array ) . toString ( 'base64' ) ;
2225const hexFromUint8Array = uint8ArrayToHex ( largeUint8Array ) ;
@@ -25,6 +28,8 @@ const suite = new benchmark.Suite();
2528
2629suite . add ( 'isUint8Array' , ( ) => isUint8Array ( largeUint8Array ) ) ;
2730
31+ suite . add ( 'compareUint8Arrays' , ( ) => compareUint8Arrays ( largeUint8Array , largeUint8ArrayDuplicate ) ) ;
32+
2833suite . add ( 'concatUint8Arrays with 2 arrays' , ( ) => concatUint8Arrays ( [ largeUint8Array , largeUint8Array ] ) ) ;
2934
3035suite . add ( 'concatUint8Arrays with 3 arrays' , ( ) => concatUint8Arrays ( [ largeUint8Array , largeUint8Array ] ) ) ;
Original file line number Diff line number Diff line change @@ -79,26 +79,15 @@ export function compareUint8Arrays(a, b) {
7979 const length = Math . min ( a . length , b . length ) ;
8080
8181 for ( let index = 0 ; index < length ; index ++ ) {
82- if ( a [ index ] < b [ index ] ) {
83- return - 1 ;
84- }
85-
86- if ( a [ index ] > b [ index ] ) {
87- return 1 ;
82+ const diff = a [ index ] - b [ index ] ;
83+ if ( diff !== 0 ) {
84+ return Math . sign ( diff ) ;
8885 }
8986 }
9087
9188 // At this point, all the compared elements are equal.
9289 // The shorter array should come first if the arrays are of different lengths.
93- if ( a . length > b . length ) {
94- return 1 ;
95- }
96-
97- if ( a . length < b . length ) {
98- return - 1 ;
99- }
100-
101- return 0 ;
90+ return Math . sign ( a . length - b . length ) ;
10291}
10392
10493const cachedDecoder = new globalThis . TextDecoder ( ) ;
You can’t perform that action at this time.
0 commit comments