Commit 23a08ab
committed
Optimize Internet username, email, password, and base64
- username: pick the username shape (first-name-only vs first+last)
before generating names, so the discarded variant is never built.
- password: reuse frozen digit/special-char tables and grow the
character bag in place with concat instead of += (which copies the
whole array each time).
- base64: reuse frozen urlsafe/standard character tables and append
into a pre-sized String instead of building an intermediate array.
- sanitize_email_local_part: replace invalid characters with a single
gsub against a character-class regex instead of rebuilding the
allowed-character array and scanning it per character.
Benchmark (Ruby 3.4.9, arm64-darwin25, benchmark-ips):
require 'benchmark/ips'
require 'faker'
Benchmark.ips do |x|
x.config(warmup: 1, time: 2)
x.report('username') { Faker::Internet.username }
x.report('email') { Faker::Internet.email }
x.report('password') { Faker::Internet.password }
x.report('base64') { Faker::Internet.base64 }
end
Results:
main: username 41.048k (+/-14.7%) i/s
email 21.683k (+/- 0.7%) i/s
password 394.792k (+/- 4.8%) i/s
base64 183.509k (+/- 2.4%) i/s
this commit: username 79.313k (+/-18.2%) i/s (~1.9x)
email 37.246k (+/- 1.3%) i/s (~1.7x)
password 451.683k (+/- 1.0%) i/s (~1.14x)
base64 462.621k (+/- 0.5%) i/s (~2.5x)1 parent 9b07803 commit 23a08ab
1 file changed
Lines changed: 37 additions & 30 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
18 | 18 | | |
19 | 19 | | |
20 | 20 | | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
21 | 38 | | |
22 | 39 | | |
23 | 40 | | |
| |||
96 | 113 | | |
97 | 114 | | |
98 | 115 | | |
99 | | - | |
100 | | - | |
101 | | - | |
102 | | - | |
103 | | - | |
104 | | - | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
105 | 125 | | |
106 | 126 | | |
107 | 127 | | |
| |||
154 | 174 | | |
155 | 175 | | |
156 | 176 | | |
157 | | - | |
| 177 | + | |
158 | 178 | | |
159 | | - | |
| 179 | + | |
160 | 180 | | |
161 | | - | |
| 181 | + | |
162 | 182 | | |
163 | 183 | | |
164 | 184 | | |
165 | 185 | | |
166 | | - | |
| 186 | + | |
167 | 187 | | |
168 | 188 | | |
169 | 189 | | |
170 | | - | |
| 190 | + | |
171 | 191 | | |
172 | | - | |
| 192 | + | |
173 | 193 | | |
174 | 194 | | |
175 | 195 | | |
| |||
528 | 548 | | |
529 | 549 | | |
530 | 550 | | |
531 | | - | |
532 | | - | |
533 | | - | |
534 | | - | |
535 | | - | |
536 | | - | |
537 | | - | |
538 | | - | |
| 551 | + | |
| 552 | + | |
| 553 | + | |
| 554 | + | |
539 | 555 | | |
540 | 556 | | |
541 | 557 | | |
| |||
562 | 578 | | |
563 | 579 | | |
564 | 580 | | |
565 | | - | |
566 | | - | |
567 | | - | |
568 | | - | |
569 | | - | |
570 | | - | |
571 | | - | |
572 | | - | |
573 | | - | |
574 | | - | |
| 581 | + | |
575 | 582 | | |
576 | 583 | | |
577 | 584 | | |
| |||
0 commit comments