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

Switch to FNV-1a hash for CSS identifiers #205

Merged
merged 5 commits into from
Nov 8, 2024
Merged

Conversation

jantimon
Copy link
Collaborator

@jantimon jantimon commented Nov 8, 2024

This PR replaces our current CSS identifier hashing implementation with FNV-1a (Fowler-Noll-Vo), a fast and platform-independent hash function that is better suited for generating CSS identifiers.

Changes

  • Replace custom hash implementation with FNV-1a algorithm
  • Improve CSS identifier generation to ensure valid CSS class names
  • Update tests with deterministic hash values
  • Update snapshots to reflect new hash output format

Why FNV-1a?

The FNV-1a hash algorithm is an ideal choice for this use case because:

  1. Deterministic Output: Guarantees consistent hashes across:

    • Different operating systems (Windows/Mac/Linux)
    • Different architectures (x86/ARM/WASM)
    • Different builds and process executions
  2. Performance: FNV-1a is:

    • Simple and fast (uses basic bitwise operations)
    • Memory efficient (operates on bytes directly)
    • Well-suited for short strings like class names
  3. Distribution: Provides excellent distribution characteristics for:

    • Short strings (like component names)
    • Similar strings (like nested selectors)
    • Small changes in input

CSS Identifier Format

The new hash format:

- y_17k2ec6  // old format
+ ymdkqtp     // new format

Changes to identifier generation:

  • Always starts with a lowercase letter (CSS requirement)
  • Uses 6 characters for consistent length
  • Utilizes base62 encoding (a-z, A-Z, 0-9) for maximum readability
  • Maintains uniqueness while being shorter

Copy link

codspeed-hq bot commented Nov 8, 2024

CodSpeed Performance Report

Merging #205 will not alter performance

Comparing hashing-switch-to-fnv1a_64 (f9d41fb) with main (da220f2)

Summary

✅ 2 untouched benchmarks

Copy link

changeset-bot bot commented Nov 8, 2024

🦋 Changeset detected

Latest commit: f9d41fb

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
yak-swc Minor
next-yak Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@jantimon jantimon requested a review from Mad-Kat November 8, 2024 12:44
@jantimon jantimon merged commit 7a080ab into main Nov 8, 2024
7 checks passed
@jantimon jantimon deleted the hashing-switch-to-fnv1a_64 branch November 8, 2024 12:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants