AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |
Back to Blog
Random uuid generator javascript1/4/2024 ![]() ![]() Running on my benchmark server locally, the uuid module has a mean execution time of about 1030 nanoseconds per UUID, with a minimum of 640 and a maximum of 870399. The h1 histogram shows the results for the uuid module. These are the only fixed values in a random UUID - all other bits in the sequence are randomly generated.Ĭopy to Clipboard Syntax Highlighter import ) The reserved field identifies the variant which is encoded into the clock-seq-and-reserved field in the eighth byte in the UUID. ![]() As described in RFC 4122, the version is “in the most significant 4 bits of the timestamp (bits 4 through 7 of the time_high_and_version field)”, which - if it’s not clear - are the four most significant bits of the sixth byte in the UUID. The version and reserved fields in the UUID identify the layout and type. Of particular note in this structure are the “version” and “reserved” bits (the time-high-and-version and clock-seq-and-reserved fields in the structure definition above). They actually have a structure as defined by RFC 4122:Ĭopy to Clipboard Syntax Highlighter UUID = time-low "-" time-mid "-" UUIDs aren’t simply a sequence of hex-encoded digits. It’s also worth knowing that uuid module maintainers helped us to review the new API that landed in Node.js core). (It’s important to note that the uuid module is not going anywhere. The web platform is working to standardise on a crypto.randomUUID() API that is common across environments.Implementing random UUID generation directly in Node.js is significantly faster.Philosophically, functionality that is found everywhere really ought to be part of the standard library of the platform. ![]() It’s a fair question, with three specific answers: So if uuid is ubiquitous, why add uuid generation to Node.js itself? I’ve never seen a production Node.js application that does not have uuid in its dependency tree, and I consider it to be among the most important dependencies in the ecosystem. Historically in Node.js, if you’ve wanted to generate UUIDs, the go-to module on npm has always been the appropriately named uuid module, a small and useful piece of code that is downloaded over 50 million times per week. The irony is that, with the complex definitions and variations that do exist, the random UUID (so-called “version 4 UUIDs”) is by far the most popular and widely used. They don’t realise there’s actually an IETF RFC detailing the construction and format of multiple variations of UUID - all of which share a common serialisation and structure with significant variations on exactly how the bytes are derived. ![]() Most developers look at them and assume they’re nothing more than a random sequence of hex-encoded bytes. Universally Unique Identifiers (UUIDs) are surprisingly complex little structures. We introduced you to Node.js’s new Web Cryptography API implementation and the new support for the HKDF key derivation scheme previously, and in this post, we discuss two powerful new capabilities for generating random UUIDs and random prime numbers: Generating random UUIDs Much has been happening in the Node.js crypto subsystem lately. The generateRandomUUID page on GitHub has the code and a link to the generateRandomUUID sample page that runs in your browser and generates a new UUID every time it's loaded.īecause it's so small, I've included the code here for reference: // offers powerful new capabilities for generating random UUIDs and random prime numbers. My UUID generator uses crypto.getRandomValues() for the best randomness and tries to do as little extra work as possible for efficiency. (For more on randomness in JavaScript, see my post about polyfilling Math.random().) This was inspired by code I saw for the same purpose that was not especially efficient - or random. A few months ago, I dashed out a JavaScript function to efficiently generate a random (version 4) UUID per RFC 4122. ![]()
0 Comments
Read More
Leave a Reply. |