@@ -3,21 +3,11 @@ import rng from './rng.js';
33import { unsafeStringify } from './stringify.js' ;
44import { UUIDTypes , Version4Options } from './types.js' ;
55
6- function v4 ( options ?: Version4Options , buf ?: undefined , offset ?: number ) : string ;
7- function v4 < TBuf extends Uint8Array = Uint8Array > (
8- options : Version4Options | undefined ,
9- buf : TBuf ,
10- offset ?: number
11- ) : TBuf ;
12- function v4 < TBuf extends Uint8Array = Uint8Array > (
6+ function _v4 < TBuf extends Uint8Array = Uint8Array > (
137 options ?: Version4Options ,
148 buf ?: TBuf ,
159 offset ?: number
1610) : UUIDTypes < TBuf > {
17- if ( native . randomUUID && ! buf && ! options ) {
18- return native . randomUUID ( ) ;
19- }
20-
2111 options = options || { } ;
2212
2313 const rnds = options . random ?? options . rng ?.( ) ?? rng ( ) ;
@@ -46,4 +36,26 @@ function v4<TBuf extends Uint8Array = Uint8Array>(
4636 return unsafeStringify ( rnds ) ;
4737}
4838
39+ function v4 ( options ?: Version4Options , buf ?: undefined , offset ?: number ) : string ;
40+ function v4 < TBuf extends Uint8Array = Uint8Array > (
41+ options : Version4Options | undefined ,
42+ buf : TBuf ,
43+ offset ?: number
44+ ) : TBuf ;
45+ function v4 < TBuf extends Uint8Array = Uint8Array > (
46+ options ?: Version4Options ,
47+ buf ?: TBuf ,
48+ offset ?: number
49+ ) : UUIDTypes < TBuf > {
50+ if ( native . randomUUID && ! buf && ! options ) {
51+ return native . randomUUID ( ) ;
52+ }
53+
54+ // Putting tail-code that could just go inline here in a separate function
55+ // allows for compiler optimizations that dramatically improve performance.
56+ //
57+ // REF: https://github.com/uuidjs/uuid/issues/892
58+ return _v4 ( options , buf , offset ) ;
59+ }
60+
4961export default v4 ;
0 commit comments