Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Random.hex for base component unique ID #10780

Merged
merged 1 commit into from
Jun 7, 2024
Merged

Conversation

aduth
Copy link
Member

@aduth aduth commented Jun 7, 2024

🛠 Summary of changes

Updates BaseComponent to use Random.hex instead of SecureRandom.hex.

These IDs are not security-sensitive, and merely need to be reasonably unique within instances of the component of a single request, to satisfy HTML validation of unique IDs across elements (ref).

Benchmark:

require 'benchmark/ips'
require 'securerandom'

Benchmark.ips do |x|
  x.report('SecureRandom.hex') do
    SecureRandom.hex(4)
  end

  x.report('Random.hex') do
    Random.hex(4)
  end

  x.compare!
end

Results:

Warming up --------------------------------------
    SecureRandom.hex   317.139k i/100ms
          Random.hex   506.735k i/100ms
Calculating -------------------------------------
    SecureRandom.hex      3.170M (± 0.4%) i/s -     15.857M in   5.002178s
          Random.hex      5.075M (± 0.2%) i/s -     25.843M in   5.092296s

Comparison:
          Random.hex:  5075034.7 i/s
    SecureRandom.hex:  3170062.9 i/s - 1.60x  slower

📜 Testing Plan

rspec spec/components/base_component_spec.rb

changelog: Internal, Performance, Optimize performance of UI component ID generation
@aduth aduth merged commit 80ec475 into main Jun 7, 2024
2 checks passed
@aduth aduth deleted the aduth-component-id-random branch June 7, 2024 18:54
brandemix pushed a commit to brandemix/18F-identity-idp that referenced this pull request Jun 17, 2024
changelog: Internal, Performance, Optimize performance of UI component ID generation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants