Skip to content

Instantly share code, notes, and snippets.

@swimauger
Last active February 2, 2021 18:22
Show Gist options
  • Save swimauger/50d2776804c27d0084340d5d3ae85a44 to your computer and use it in GitHub Desktop.
Save swimauger/50d2776804c27d0084340d5d3ae85a44 to your computer and use it in GitHub Desktop.
UUID Generator v2 : Using Generators, yield, and 999,999,999,999 100% unique ids
function* UUIDGenerator() {
for (let i = 100000000000; true; i++) {
yield `${`${i}`.substr(0, 4)}-${`${i}`.substr(4, 4)}-${`${i}`.substr(8, 4)}`;
}
}
const uuid2 = UUIDGenerator();
for (let i = 0; i < 10001; i++) {
console.log(uuid2.next().value) // 1000-0000-0000 -> 1000-0001-0000
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment