Skip to content

Instantly share code, notes, and snippets.

@emilbayes
Created July 30, 2019 11:47
Show Gist options
  • Save emilbayes/bd9523a801308b27c2c6568f5d275288 to your computer and use it in GitHub Desktop.
Save emilbayes/bd9523a801308b27c2c6568f5d275288 to your computer and use it in GitHub Desktop.
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