Created
July 30, 2019 11:47
-
-
Save emilbayes/bd9523a801308b27c2c6568f5d275288 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function generator () { | |
var lastTime = Date.now() | |
var cnt = 0 | |
return function () { | |
var currentTime = Date.now() | |
if (currentTime !== lastTime) { | |
lastTime = currentTime | |
cnt = 0 | |
} | |
// move lastTime up 10 bits, meaning there can be 2^11 - 1 | |
// ids in the same milli second. Note that multiplied time | |
// must be a Number.isSafeInteger to not loose precision | |
return (lastTime * 2048) + cnt++ | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment