engine
engine copied to clipboard
Faster GUID generator
Benchmark: https://codepen.io/munsocket/pen/QWyLKMB?editors=1010
I confirm I have read the contributing guidelines and not signed Contributor License Agreement
Thanks for this @munsocket! Where is the algorithm from/what is it called for us to check against please?
The algorithm is pretty common, we a creating look up table with alphabet and than iterating through it randomly.
It’s based on this stackoverflow https://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid https://jcward.com/UUID.js But with fixed range.
For performance, please run tests many times to warm up the V8.
Already created variable count, if it will be bigger than some number it will be wamed up. Not sure that this code will be “hot” in PlayCanvas for v8 enough to be optimized, only if somebody creating 3d objects in loop or in big scenes. But in general regex solutions is slower.
I just ran the benchmark and I saw this:

So there's tjs, new and new2. Which one is this PR implementing? The second has a NaN in it (from my run of the benchmark), despite being the fastest. And new2 doesn't print the GUID.
Sorry for not being clear, I am tested different approaches (crypto.getRandomValues and time based). Here more clear benchmark that show previous and new GUID generators https://codepen.io/munsocket/pen/QWyLKMB

PS: Seems that I found and error, better to stick to Jeff Ward MIT version that is used in Three.js too.
Cool, this is great @munsocket, thanks! You might have missed this link at the top - would you be able to complete that so I can merge, please? Thanks!
PS: Seems that I found and error, better to stick to Jeff Ward MIT version that is used in Three.js too.
Just to confirm .. has this error been fixed in your following commit? Or is this still TODO.
Yes fixed. Your contribution license is really weird because it’s asking my phone number and county. I am trying to keep anonymity and not planning to share this information, so PR can be closed.
Can't we just close it then?
- It adds more code and it looks more complicated
- I've never seen this in any profiling session as a bottleneck
Agreed and closing.