Commit be8eed4
Make Base.numerify single-pass and allocation-free per digit
Replace the sub-then-gsub double scan with a single gsub whose block
tracks whether the leading (non-zero) digit has been emitted, and look
digits up in a frozen DIGITS table instead of calling Integer#to_s for
every replaced 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("numerify('###-###-####')") { Faker::Base.numerify('###-###-####') }
x.report('PhoneNumber.phone_number') { Faker::PhoneNumber.phone_number }
end
Results:
main: numerify('###-###-####') 486.140k (+/- 4.8%) i/s
PhoneNumber.phone_number 100.815k (+/-15.3%) i/s
this commit: numerify('###-###-####') 601.743k (+/- 5.2%) i/s (~1.24x)
PhoneNumber.phone_number 104.568k (+/-14.7%) i/s (within noise)
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>1 parent 9b07803 commit be8eed4
1 file changed
Lines changed: 12 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
58 | 58 | | |
59 | 59 | | |
60 | 60 | | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
61 | 64 | | |
62 | 65 | | |
63 | 66 | | |
| |||
66 | 69 | | |
67 | 70 | | |
68 | 71 | | |
69 | | - | |
70 | | - | |
71 | | - | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
72 | 81 | | |
73 | 82 | | |
74 | 83 | | |
| |||
0 commit comments