The GUID is in the following format XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX.For the rest of the cases, the addition of zeros will not harm the result because it is cutting it off anyway. That's why we needed to add nine zeros to it ( "0"+"000000000" or "1"+"000000000"), and then cutting it off from the second index (third character) with a length of eight characters. The reason for adding exactly nine zeros is because of the worse case scenario, which is when the Math.random() function will return exactly 0 or 1 (probability of 1/10^16 for each one of them).That's why I'm appending to this string "000000000" (a string with nine zeros) and then cutting it off with substr() function to make it nine characters exactly (filling zeros to the right). Shorter number (for example 0.4363), due to zeros at the end (from the example above, actually the number is 0.4363000000000000). Sometimes the Math.random() function will return.To test the performance, you can run this code: console.time('t') Ħfb7687f) and get a string with eight hexadecimal Ten million executions of this implementation take just 32.5 seconds, which is the fastest I've ever seen in a browser (the only solution without loops/iterations). It does not generate a standard-compliant GUID. This is the fastest GUID-like string generator method in the format XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX. Other answers cover important topics such as collisions and truly random numbers, which are important for generating good UUIDs. I hope you enjoyed this mad ride through code optimization!īe advised: my primary goal was to show and teach potential optimization strategies. I hope my logic is correct - it's very easy to make a mistake in this kind of tedious bit work. The whole trick is expressing it in string format with RFC compliance, and it's most tightly accomplished with 16 bytes of random data, an unrolled loop and lookup table. The funny thing is, generating 16 bytes of random data is the easy part. But with four variables assigned random data up front, then using the lookup table, and applying the proper RFC bits, this version smokes them all: I tried this once with a single random variable, r, that I kept reassigning, and performance tanked. Since we're looping a fixed number of times, we can technically write this all out by hand. The final optimization technique to apply - unroll the loop. Though it had fewer iterations, the inner logic was complicated by the increased processing, and it performed the same on desktop, and only ~10% faster on mobile. I tried an e6() that processes 16-bits at a time, still using the 256-element LUT, and it showed the diminishing returns of optimization. Interested in how? I've put the full source on and on įor an explanation, let's start with broofa's code: So by my 6th iteration of optimizations, I beat the most popular answer by over 12 times, the accepted answer by over 9 times, and the fast-non-compliant answer by 2-3 times. Note: 500k iterations, results will vary by browser/CPU. Not really, but it's easy if you follow along.īut first, my results, compared to broofa, guid (the accepted answer), and the non-rfc-compliant generateQuickGuid: Desktop Android Indeed, joelpt even decided to toss out an RFC for generic GUID speed with generateQuickGUID.īut, can we get speed and RFC compliance? I say, YES! Can we maintain readability? Well. Awesome!īut if you're looking at that regular expression, those many replace() callbacks, toString()'s and Math.random() function calls (where he's only using four bits of the result and wasting the rest), you may start to wonder about performance. RFC4122 compliant, somewhat readable, and compact. Broofa's answer is pretty slick, indeed - impressively clever, really.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |