[{"data":1,"prerenderedAt":598},["ShallowReactive",2],{"guide-uuid-generation-performance":3},{"id":4,"title":5,"body":6,"date":588,"description":589,"extension":590,"meta":591,"navigation":592,"path":593,"readingTime":594,"seo":595,"stem":596,"__hash__":597},"guides\u002Fguides\u002Fuuid-generation-performance.md","UUID Generation Performance Compared",{"type":7,"value":8,"toc":568},"minimark",[9,13,18,23,26,37,41,44,50,54,57,63,67,70,153,157,160,164,171,175,178,224,227,231,234,240,243,301,305,308,312,315,384,387,391,449,459,466,470,528,532,551,555,564],[10,11,12],"p",{},"Choosing an ID generation strategy involves trade-offs between speed, uniqueness guarantees, storage size, and sortability. UUID v4, UUID v7, and NanoID are three of the most widely used options, each with distinct performance characteristics. This guide benchmarks their generation speed and compares their collision resistance so you can pick the right tool for your throughput requirements.",[14,15,17],"h2",{"id":16},"the-three-contenders","The Three Contenders",[19,20,22],"h3",{"id":21},"uuid-v4","UUID v4",[10,24,25],{},"122 random bits generated by a cryptographically secure random number generator (CSPRNG). Fully random, no sequential component.",[27,28,33],"pre",{"className":29,"code":31,"language":32},[30],"language-text","f47ac10b-58cc-4372-a567-0e02b2c3d479\n","text",[34,35,31],"code",{"__ignoreMap":36},"",[19,38,40],{"id":39},"uuid-v7","UUID v7",[10,42,43],{},"48-bit millisecond timestamp + 74 random bits (after version and variant markers). Monotonically increasing within a millisecond, with random bits for uniqueness.",[27,45,48],{"className":46,"code":47,"language":32},[30],"0192d6c4-a801-7abc-8000-1a2b3c4d5e6f\n",[34,49,47],{"__ignoreMap":36},[19,51,53],{"id":52},"nanoid","NanoID",[10,55,56],{},"21-character URL-safe string using a 64-character alphabet. 126 bits of entropy from a CSPRNG. Compact and web-friendly.",[27,58,61],{"className":59,"code":60,"language":32},[30],"V1StGXR8_Z5jdHi6B-myT\n",[34,62,60],{"__ignoreMap":36},[14,64,66],{"id":65},"generation-speed-benchmarks","Generation Speed Benchmarks",[10,68,69],{},"The following benchmarks measure single-threaded ID generation in Node.js 22 on an x86-64 machine. Each test generates 1 million IDs using the most popular library for each format.",[71,72,73,95],"table",{},[74,75,76],"thead",{},[77,78,79,83,86,89,92],"tr",{},[80,81,82],"th",{},"Format",[80,84,85],{},"Library",[80,87,88],{},"Time (1M IDs)",[80,90,91],{},"IDs\u002Fsecond",[80,93,94],{},"Relative",[96,97,98,118,135],"tbody",{},[77,99,100,103,109,112,115],{},[101,102,22],"td",{},[101,104,105,108],{},[34,106,107],{},"uuid"," (v10)",[101,110,111],{},"1,420 ms",[101,113,114],{},"~704,000",[101,116,117],{},"Baseline",[77,119,120,122,126,129,132],{},[101,121,40],{},[101,123,124,108],{},[34,125,107],{},[101,127,128],{},"1,380 ms",[101,130,131],{},"~725,000",[101,133,134],{},"+3%",[77,136,137,139,144,147,150],{},[101,138,53],{},[101,140,141,143],{},[34,142,52],{}," (v5)",[101,145,146],{},"920 ms",[101,148,149],{},"~1,087,000",[101,151,152],{},"+54%",[19,154,156],{"id":155},"why-nanoid-is-fastest","Why NanoID Is Fastest",[10,158,159],{},"NanoID uses a custom alphabet and a faster random byte generation path. Instead of formatting 16 bytes into hex-with-hyphens, it maps random bytes directly to URL-safe characters. The shorter output string also reduces memory allocation overhead.",[19,161,163],{"id":162},"why-uuid-v7-beats-uuid-v4","Why UUID v7 Beats UUID v4",[10,165,166,167,170],{},"Counterintuitively, UUID v7 generation is slightly faster than v4 in most implementations. The reason: ",[34,168,169],{},"Date.now()"," (a single OS call) replaces 2 bytes of CSPRNG output. Reading the system clock is cheaper than generating additional random bytes.",[19,172,174],{"id":173},"multi-threaded-scaling","Multi-Threaded Scaling",[10,176,177],{},"On a 4-core system with worker threads:",[71,179,180,192],{},[74,181,182],{},[77,183,184,186,189],{},[80,185,82],{},[80,187,188],{},"4 threads × 1M IDs",[80,190,191],{},"Total IDs\u002Fsecond",[96,193,194,204,214],{},[77,195,196,198,201],{},[101,197,22],{},[101,199,200],{},"5,400 ms",[101,202,203],{},"~741,000",[77,205,206,208,211],{},[101,207,40],{},[101,209,210],{},"5,200 ms",[101,212,213],{},"~769,000",[77,215,216,218,221],{},[101,217,53],{},[101,219,220],{},"3,100 ms",[101,222,223],{},"~1,290,000",[10,225,226],{},"UUID v4 and v7 scale nearly linearly because their randomness is independent per thread. NanoID scales slightly better due to lower per-ID allocation cost.",[14,228,230],{"id":229},"collision-resistance","Collision Resistance",[10,232,233],{},"Collision probability depends on the number of entropy bits and the total number of IDs generated. The Birthday Problem approximation gives:",[10,235,236],{},[237,238,239],"strong",{},"P(collision) ≈ n² \u002F (2 × 2^k)",[10,241,242],{},"where n is the number of IDs and k is the entropy bit count.",[71,244,245,260],{},[74,246,247],{},[77,248,249,251,254,257],{},[80,250,82],{},[80,252,253],{},"Entropy Bits",[80,255,256],{},"IDs for 50% Collision",[80,258,259],{},"IDs for 10⁻⁹ Collision",[96,261,262,275,288],{},[77,263,264,266,269,272],{},[101,265,22],{},[101,267,268],{},"122",[101,270,271],{},"2.17 × 10¹⁸",[101,273,274],{},"5.28 × 10¹⁴",[77,276,277,279,282,285],{},[101,278,40],{},[101,280,281],{},"74 (within 1ms)",[101,283,284],{},"4.2 × 10¹⁰",[101,286,287],{},"10.2 × 10⁶",[77,289,290,292,295,298],{},[101,291,53],{},[101,293,294],{},"126",[101,296,297],{},"8.6 × 10¹⁸",[101,299,300],{},"2.1 × 10¹⁵",[19,302,304],{"id":303},"the-uuid-v7-subtlety","The UUID v7 Subtlety",[10,306,307],{},"UUID v7's collision resistance depends on the time window. Within the same millisecond, only 74 random bits separate two IDs. If your system generates more than ~10 million IDs per millisecond, the collision probability within that millisecond exceeds 10⁻⁹. In practice, this threshold is unreachable for any real application, but it is worth noting for ultra-high-throughput scenarios.",[19,309,311],{"id":310},"nanoid-custom-alphabets","NanoID Custom Alphabets",[10,313,314],{},"NanoID lets you reduce the alphabet size for shorter IDs, but this reduces entropy:",[27,316,320],{"className":317,"code":318,"language":319,"meta":36,"style":36},"language-javascript shiki shiki-themes github-light github-dark","import { customAlphabet } from 'nanoid';\n\u002F\u002F 8-char ID with 36-char alphabet: 41.6 bits of entropy\nconst shortId = customAlphabet('0123456789abcdefghijklmnopqrstuvwxyz', 8);\n","javascript",[34,321,322,345,352],{"__ignoreMap":36},[323,324,327,331,335,338,342],"span",{"class":325,"line":326},"line",1,[323,328,330],{"class":329},"szBVR","import",[323,332,334],{"class":333},"sVt8B"," { customAlphabet } ",[323,336,337],{"class":329},"from",[323,339,341],{"class":340},"sZZnC"," 'nanoid'",[323,343,344],{"class":333},";\n",[323,346,348],{"class":325,"line":347},2,[323,349,351],{"class":350},"sJ8bj","\u002F\u002F 8-char ID with 36-char alphabet: 41.6 bits of entropy\n",[323,353,355,358,362,365,369,372,375,378,381],{"class":325,"line":354},3,[323,356,357],{"class":329},"const",[323,359,361],{"class":360},"sj4cs"," shortId",[323,363,364],{"class":329}," =",[323,366,368],{"class":367},"sScJk"," customAlphabet",[323,370,371],{"class":333},"(",[323,373,374],{"class":340},"'0123456789abcdefghijklmnopqrstuvwxyz'",[323,376,377],{"class":333},", ",[323,379,380],{"class":360},"8",[323,382,383],{"class":333},");\n",[10,385,386],{},"A 41.6-bit ID reaches 50% collision at ~33 million IDs — fine for session tokens, dangerous for primary keys.",[14,388,390],{"id":389},"storage-and-transport-size","Storage and Transport Size",[71,392,393,408],{},[74,394,395],{},[77,396,397,399,402,405],{},[80,398,82],{},[80,400,401],{},"Bytes (binary)",[80,403,404],{},"Bytes (string)",[80,406,407],{},"Characters",[96,409,410,424,436],{},[77,411,412,415,418,421],{},[101,413,414],{},"UUID",[101,416,417],{},"16",[101,419,420],{},"36 (with hyphens)",[101,422,423],{},"36",[77,425,426,428,431,434],{},[101,427,53],{},[101,429,430],{},"—",[101,432,433],{},"21",[101,435,433],{},[77,437,438,441,444,447],{},[101,439,440],{},"INT (auto-increment)",[101,442,443],{},"4",[101,445,446],{},"~10",[101,448,446],{},[10,450,451,452,455,456,458],{},"When stored as text in a database column (",[34,453,454],{},"VARCHAR(36)","), UUIDs consume 36 bytes per row — 9x more than a 4-byte integer. Stored as ",[34,457,414],{}," type (PostgreSQL), the binary representation uses 16 bytes.",[10,460,461,462,465],{},"NanoID's 21-character string uses 21 bytes as ",[34,463,464],{},"VARCHAR(21)",", but it cannot be stored as a compact binary type without custom encoding.",[14,467,469],{"id":468},"choosing-the-right-format","Choosing the Right Format",[71,471,472,482],{},[74,473,474],{},[77,475,476,479],{},[80,477,478],{},"Requirement",[80,480,481],{},"Best Choice",[96,483,484,491,498,505,512,520],{},[77,485,486,489],{},[101,487,488],{},"Maximum generation speed",[101,490,53],{},[77,492,493,496],{},[101,494,495],{},"Database primary key (indexed)",[101,497,40],{},[77,499,500,503],{},[101,501,502],{},"Distributed system, no coordination",[101,504,40],{},[77,506,507,510],{},[101,508,509],{},"URL-safe, compact IDs",[101,511,53],{},[77,513,514,517],{},[101,515,516],{},"Maximum collision resistance",[101,518,519],{},"NanoID \u002F UUID v4",[77,521,522,525],{},[101,523,524],{},"Backward compatibility with existing UUID columns",[101,526,527],{},"UUID v7 (drop-in replacement)",[14,529,531],{"id":530},"key-takeaways","Key Takeaways",[533,534,535,539,542,545,548],"ul",{},[536,537,538],"li",{},"NanoID generates IDs roughly 50% faster than UUID v4\u002Fv7 due to simpler formatting and shorter output.",[536,540,541],{},"UUID v7 is slightly faster than UUID v4 because the clock read replaces two bytes of random generation.",[536,543,544],{},"All three formats provide adequate collision resistance for practical use — even UUID v7's within-millisecond entropy exceeds 10 million IDs before collision risk rises meaningfully.",[536,546,547],{},"UUID v7 is the best primary key choice because it combines sortability (time-ordered) with uniqueness.",[536,549,550],{},"NanoID is ideal for URL-safe, compact identifiers where database indexing is not the primary concern.",[14,552,554],{"id":553},"try-it-yourself","Try It Yourself",[10,556,557,558,563],{},"Generate your own UUIDs and compare v4 vs v7 side by side using the ",[559,560,562],"a",{"href":561},"\u002Ftools\u002Fuuid-generator","UUID Generator",". Notice how v7 values sort chronologically while v4 values appear fully random.",[565,566,567],"style",{},"html pre.shiki code .szBVR, html code.shiki .szBVR{--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sVt8B, html code.shiki .sVt8B{--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sZZnC, html code.shiki .sZZnC{--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .sJ8bj, html code.shiki .sJ8bj{--shiki-default:#6A737D;--shiki-dark:#6A737D}html pre.shiki code .sj4cs, html code.shiki .sj4cs{--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sScJk, html code.shiki .sScJk{--shiki-default:#6F42C1;--shiki-dark:#B392F0}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}",{"title":36,"searchDepth":347,"depth":347,"links":569},[570,575,580,584,585,586,587],{"id":16,"depth":347,"text":17,"children":571},[572,573,574],{"id":21,"depth":354,"text":22},{"id":39,"depth":354,"text":40},{"id":52,"depth":354,"text":53},{"id":65,"depth":347,"text":66,"children":576},[577,578,579],{"id":155,"depth":354,"text":156},{"id":162,"depth":354,"text":163},{"id":173,"depth":354,"text":174},{"id":229,"depth":347,"text":230,"children":581},[582,583],{"id":303,"depth":354,"text":304},{"id":310,"depth":354,"text":311},{"id":389,"depth":347,"text":390},{"id":468,"depth":347,"text":469},{"id":530,"depth":347,"text":531},{"id":553,"depth":347,"text":554},"2026-05-28","Benchmark v4 vs v7 vs NanoID generation speed and collision resistance at scale.","md",{"immutable":592},true,"\u002Fguides\u002Fuuid-generation-performance",6,{"title":5,"description":589},"guides\u002Fuuid-generation-performance","LC45Rx5MapbmSPk5bOO8FD5UWyPmu60rz6zruSdEpvw",1780401338167]