Collisions when generating UUIDs in JavaScript?

前端 未结 6 1853
遥遥无期
遥遥无期 2020-11-28 20:12

This relates to this question. I am using the code below from this answer to generate UUID in JavaScript:

\'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx\'.replace(/[         


        
6条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-28 20:50

    Indeed there are collisions but only under Google Chrome. Check out my experience on the topic here

    http://devoluk.com/google-chrome-math-random-issue.html

    (Link broken as of 2019. Archive link: https://web.archive.org/web/20190121220947/http://devoluk.com/google-chrome-math-random-issue.html.)

    Seems like collisions only happen on the first few calls of Math.random. Cause if you just run the createGUID / testGUIDs method above (which obviously was the first thing I tried) it just works with no collisions whatsoever.

    So to make a full test one needs to restart Google Chrome, generate 32 byte, restart Chrome, generate, restart, generate...

提交回复
热议问题