@@ -12,13 +12,29 @@ export class InternetModule {
1212 * @example faker.internet.email() // 'john.doe@example.com'
1313 */
1414 email ( options ?: { firstName ?: string , lastName ?: string , provider ?: string } ) : string {
15- // Use all gendered names combined for email generation
16- const allFirstNames = [
17- ...this . locale . person . firstNameMale ,
18- ...this . locale . person . firstNameFemale ,
19- ...( this . locale . person . firstNameNeutral || [ ] ) ,
20- ]
21- const firstName = options ?. firstName ?? this . random . arrayElement ( allFirstNames ) . toLowerCase ( )
15+ // Optimized: avoid array spreading by selecting from combined length
16+ let firstName : string
17+ if ( options ?. firstName ) {
18+ firstName = options . firstName
19+ }
20+ else {
21+ const maleLen = this . locale . person . firstNameMale . length
22+ const femaleLen = this . locale . person . firstNameFemale . length
23+ const neutralLen = this . locale . person . firstNameNeutral ?. length || 0
24+ const totalLen = maleLen + femaleLen + neutralLen
25+ const index = this . random . int ( 0 , totalLen - 1 )
26+
27+ if ( index < maleLen ) {
28+ firstName = this . locale . person . firstNameMale [ index ] . toLowerCase ( )
29+ }
30+ else if ( index < maleLen + femaleLen ) {
31+ firstName = this . locale . person . firstNameFemale [ index - maleLen ] . toLowerCase ( )
32+ }
33+ else {
34+ firstName = this . locale . person . firstNameNeutral ! [ index - maleLen - femaleLen ] . toLowerCase ( )
35+ }
36+ }
37+
2238 const lastName = options ?. lastName ?? this . random . arrayElement ( this . locale . person . lastName ) . toLowerCase ( )
2339 const provider = options ?. provider ?? this . random . arrayElement ( this . locale . internet . domainSuffix ?? [ 'com' , 'net' , 'org' ] )
2440
@@ -41,13 +57,24 @@ export class InternetModule {
4157 * @example faker.internet.freeEmail() // 'john.doe@gmail.com'
4258 */
4359 freeEmail ( ) : string {
44- // Use all gendered names combined for email generation
45- const allFirstNames = [
46- ...this . locale . person . firstNameMale ,
47- ...this . locale . person . firstNameFemale ,
48- ...( this . locale . person . firstNameNeutral || [ ] ) ,
49- ]
50- const firstName = this . random . arrayElement ( allFirstNames ) . toLowerCase ( )
60+ // Optimized: avoid array spreading by selecting from combined length
61+ const maleLen = this . locale . person . firstNameMale . length
62+ const femaleLen = this . locale . person . firstNameFemale . length
63+ const neutralLen = this . locale . person . firstNameNeutral ?. length || 0
64+ const totalLen = maleLen + femaleLen + neutralLen
65+ const index = this . random . int ( 0 , totalLen - 1 )
66+
67+ let firstName : string
68+ if ( index < maleLen ) {
69+ firstName = this . locale . person . firstNameMale [ index ] . toLowerCase ( )
70+ }
71+ else if ( index < maleLen + femaleLen ) {
72+ firstName = this . locale . person . firstNameFemale [ index - maleLen ] . toLowerCase ( )
73+ }
74+ else {
75+ firstName = this . locale . person . firstNameNeutral ! [ index - maleLen - femaleLen ] . toLowerCase ( )
76+ }
77+
5178 const lastName = this . random . arrayElement ( this . locale . person . lastName ) . toLowerCase ( )
5279 const provider = this . random . arrayElement ( this . locale . internet . freeEmail ?? [ 'gmail.com' , 'yahoo.com' , 'hotmail.com' ] )
5380
@@ -62,14 +89,30 @@ export class InternetModule {
6289 * @example faker.internet.username() // 'john_doe123'
6390 */
6491 username ( options ?: { firstName ?: string , lastName ?: string } ) : string {
65- // Use all gendered names combined for username generation
66- const allFirstNames = [
67- ...this . locale . person . firstNameMale ,
68- ...this . locale . person . firstNameFemale ,
69- ...( this . locale . person . firstNameNeutral || [ ] ) ,
70- ]
71- const firstName = options ?. firstName ?? this . random . arrayElement ( allFirstNames ) . toLowerCase ( )
72- const lastName = options ?. lastName ?? this . random . arrayElement ( this . locale . person . lastName ) . toLowerCase ( )
92+ // Optimized: avoid array spreading by selecting from combined length
93+ let firstName : string
94+ if ( options ?. firstName ) {
95+ firstName = options . firstName . toLowerCase ( )
96+ }
97+ else {
98+ const maleLen = this . locale . person . firstNameMale . length
99+ const femaleLen = this . locale . person . firstNameFemale . length
100+ const neutralLen = this . locale . person . firstNameNeutral ?. length || 0
101+ const totalLen = maleLen + femaleLen + neutralLen
102+ const index = this . random . int ( 0 , totalLen - 1 )
103+
104+ if ( index < maleLen ) {
105+ firstName = this . locale . person . firstNameMale [ index ] . toLowerCase ( )
106+ }
107+ else if ( index < maleLen + femaleLen ) {
108+ firstName = this . locale . person . firstNameFemale [ index - maleLen ] . toLowerCase ( )
109+ }
110+ else {
111+ firstName = this . locale . person . firstNameNeutral ! [ index - maleLen - femaleLen ] . toLowerCase ( )
112+ }
113+ }
114+
115+ const lastName = options ?. lastName ?. toLowerCase ( ) ?? this . random . arrayElement ( this . locale . person . lastName ) . toLowerCase ( )
73116
74117 const patterns = [
75118 `${ firstName } ${ lastName } ` ,
0 commit comments