DavidWells / DavidWells/analytics

Shrink UUID implementation

Open
#152 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
JavaScript
Stars
2.7k
Forks
267
PR merge metrics
No merged PRs in 30d

Description

Currently UUID is

function uuid() {
  var u = '',
  m = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx',
  i = 0,
  rb = Math.random() * 0xffffffff|0;

  while (i++<36) {
    var c = m [i-1],
    r = rb&0xf,
    v = c=='x' ? r : (r&0x3|0x8);

    u += (c=='-' || c=='4') ? c : v.toString(16);
    rb = i%8==0 ? Math.random() * 0xffffffff|0 : rb>>4
  }
  return u
}

Should we shrink to

function uuid(a) {
  return a
    ? (a ^ ((Math.random() * 16) >> (a / 4))).toString(16)
    : ([1e7] + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, uuid);
}

Crypto version

function uuid() {
    return ([1e7] + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, (c) => (c ^ (crypto.getRandomValues(new Uint8Array(1))[0] & (15 >> (c / 4)))).toString(16));
}

Combined using crypto.getRandomValues with fallback

function uuid(a) {
  return a
    ? (a ^ ((crypto.getRandomValues ? crypto.getRandomValues(new Uint8Array(1))[0] : Math.random() * 16) & (15 >> a / 4))).toString(16)
    : ([1e7] + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, uuid);
}

Pros? Cons? ¯_(ツ)_/¯

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No file or test is named. Start by locating the current UUID implementation and compare it with each proposed JavaScript variant, including the crypto fallback; done requires a maintainer decision on the implementation and validation that the chosen behavior remains compatible.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
analytics
Issue type
Refactor
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.