/** | |
* Fancy ID generator that creates 20-character string identifiers with the following properties: | |
* | |
* 1. They're based on timestamp so that they sort *after* any existing ids. | |
* 2. They contain 72-bits of random data after the timestamp so that IDs won't collide with other clients' IDs. | |
* 3. They sort *lexicographically* (so the timestamp is converted to characters that will sort properly). | |
* 4. They're monotonically increasing. Even if you generate more than one in the same timestamp, the | |
* latter ones will sort after the former ones. We do this by using the previous random bits | |
* but "incrementing" them by 1 (only in the case of a timestamp collision). | |
*/ | |
generatePushID = (function() { | |
// Modeled after base64 web-safe chars, but ordered by ASCII. | |
var PUSH_CHARS = '-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz'; | |
// Timestamp of last push, used to prevent local collisions if you push twice in one ms. | |
var lastPushTime = 0; | |
// We generate 72-bits of randomness which get turned into 12 characters and appended to the | |
// timestamp to prevent collisions with other clients. We store the last characters we | |
// generated because in the event of a collision, we'll use those same characters except | |
// "incremented" by one. | |
var lastRandChars = []; | |
return function() { | |
var now = new Date().getTime(); | |
var duplicateTime = (now === lastPushTime); | |
lastPushTime = now; | |
var timeStampChars = new Array(8); | |
for (var i = 7; i >= 0; i--) { | |
timeStampChars[i] = PUSH_CHARS.charAt(now % 64); | |
// NOTE: Can't use << here because javascript will convert to int and lose the upper bits. | |
now = Math.floor(now / 64); | |
} | |
if (now !== 0) throw new Error('We should have converted the entire timestamp.'); | |
var id = timeStampChars.join(''); | |
if (!duplicateTime) { | |
for (i = 0; i < 12; i++) { | |
lastRandChars[i] = Math.floor(Math.random() * 64); | |
} | |
} else { | |
// If the timestamp hasn't changed since last push, use the same random number, except incremented by 1. | |
for (i = 11; i >= 0 && lastRandChars[i] === 63; i--) { | |
lastRandChars[i] = 0; | |
} | |
lastRandChars[i]++; | |
} | |
for (i = 0; i < 12; i++) { | |
id += PUSH_CHARS.charAt(lastRandChars[i]); | |
} | |
if(id.length != 20) throw new Error('Length should be 20.'); | |
return id; | |
}; | |
})(); |
This comment has been minimized.
This comment has been minimized.
CSPRNG if you want to replace |
This comment has been minimized.
This comment has been minimized.
Port in PHP (for fun): https://gist.github.com/jeremyworboys/b5854d9591d7763c5bd7 |
This comment has been minimized.
This comment has been minimized.
Port in Python: https://gist.github.com/risent/4cab3878d995bec7d1c2 |
This comment has been minimized.
This comment has been minimized.
Port in Java: https://gist.github.com/jfbyers/d142503b2e41556b5684 |
This comment has been minimized.
This comment has been minimized.
Port in Nim (aka Nimrod) https://gist.github.com/oderwat/04b8c8f586132909e8f0 ... Quality may vary I just code in Nim for some days. |
This comment has been minimized.
This comment has been minimized.
Port in Go (done quickly). In Go playground: |
This comment has been minimized.
This comment has been minimized.
Go package: https://github.com/kjk/betterguid |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Port in C (last gen time just work in real implementation): |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Thanks everybody for all the ports! :-) |
This comment has been minimized.
This comment has been minimized.
This may not be a practical concern, but You could potentially make the algorithm a bit more robust at a relatively minor performance cost by doing something like this: var i = Math.ceil( Math.log(now) / Math.log( 64 ) );
for (; i >= 0; i--) { /* ... */ } Benchmark.js comparison
NOTE: I forgot to take the array size into account. Of course, if you want a well-known key length this won't help anything |
This comment has been minimized.
This comment has been minimized.
In case it comes in handy for someone, I wrote a quick function that converts a Push ID to Unix Time: https://gist.github.com/svincent/ae4eead8f7a97620e963 |
This comment has been minimized.
This comment has been minimized.
Port in CoffeeScript just for fun https://gist.github.com/ZeroDragon/b3c5bad5e71b0f7cea63ff53fb88e561 |
This comment has been minimized.
This comment has been minimized.
Objective-C for fun: https://gist.github.com/kcmoffat/af856ab4b605a00216d3b5f627e50a84 |
This comment has been minimized.
This comment has been minimized.
TypeScript port for production https://gist.github.com/episage/0fa8fcf71a28985197c9ba1d51f84408 |
This comment has been minimized.
This comment has been minimized.
Port in Java without using java.util.Date https://gist.github.com/RomansBermans/6f3836188427fbd3b1efcf7e6418f06d |
This comment has been minimized.
This comment has been minimized.
Tested in Google App Script. |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
I've added a package called var pushid = require('pushid')
console.log(pushid())
// -> "-KQ40rgB96epAE7LZH2W" |
This comment has been minimized.
This comment has been minimized.
Port in C#: https://gist.github.com/kiliman/ca1d9f4135078a6b24c5005113bbeea4 Includes output showing timestamp collisions. Also added a method to convert PushID back into timestamp. |
This comment has been minimized.
This comment has been minimized.
Port to MySQL stored procedure - MySQL Stored Procedure for generating Firebase Push IDs. I've found this useful for migrating MySQL databases to Firebase. |
This comment has been minimized.
This comment has been minimized.
Sorry for my ignorance but why does the generatePushID returns a function instead of the id? Could somebody kindly explain me? Thanks |
This comment has been minimized.
This comment has been minimized.
@bernatfortet the first call to the wrapped function is done for you at the bottom: This essentially makes |
This comment has been minimized.
This comment has been minimized.
Another Java port with JUnit test to verify correct multithreading operation: https://gist.github.com/swftvsn/438b4ed68619ad1f5d1c251dc3a5af6f |
This comment has been minimized.
This comment has been minimized.
Cleaned up a little with both generator for uid and also timestamp extraction method (TypeScript) https://gist.github.com/origin1tech/17eb8259084d2edab3f005c84f10d2bb |
This comment has been minimized.
This comment has been minimized.
Can you license this with something permissive please? |
This comment has been minimized.
This comment has been minimized.
thanks for all! |
This comment has been minimized.
This comment has been minimized.
+1 for @jtexp |
This comment has been minimized.
This comment has been minimized.
I would really like a license as well |
This comment has been minimized.
This comment has been minimized.
Elm 0.18.0 effect manager version. |
This comment has been minimized.
This comment has been minimized.
SublimeText 3 plugin (no numpy) |
This comment has been minimized.
This comment has been minimized.
Hi, after the code piece: |
This comment has been minimized.
This comment has been minimized.
Thread safe Java port with bug fixes and jmh benchmark: |
This comment has been minimized.
This comment has been minimized.
Could have taken me weeks to work this out on my own... thanks a bunch |
This comment has been minimized.
This comment has been minimized.
I created a Rust version here: https://github.com/silverbeak/rust-push-id I will get to documentation soon, hopefully. Some nice submissions in this thread. Kudos, all! |
This comment has been minimized.
This comment has been minimized.
@SVincent, you have your calculations wrong I think: // 64 = 2^6
// So it actually uses Math.pow ( 64, 8 ) === Math.pow ( 2, 48 )
const maxTime = Math.pow ( 2, 48 ) - 1
let now = maxTime
for ( let i = 7; i >= 0; i-- ) {
now = Math.floor ( now / 64 )
}
// ==> now === 0
const date = new Date ()
date.setTime ( maxTime )
d.toISOString ()
// ==> "+010889-08-02T05:31:50.655Z"
// This date is greater then year 10889
// It is actually somewhere in 10895 |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Im so grateful finding this |
This comment has been minimized.
This comment has been minimized.
port in elixir https://hex.pm/packages/firebase_pushid |
This comment has been minimized.
This comment has been minimized.
Did pushid changed in firebase lately? None of the thousands of ids generated contains dash (-) or underscore (_). Or anything apart from alphanumeric characters. |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Swift 4: https://gist.github.com/AhmedOS/512531b74da37f34331ecb206c77c20a |
This comment has been minimized.
This comment has been minimized.
Made a JSFiddle: https://jsfiddle.net/feLys02r/1/ It's the original script with a button to copy to clipboard. |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Port in Unity C#: https://gist.github.com/bhupiister/05dbb860e9064c43e8176b591f11e555 |
This comment has been minimized.
Port in Ruby: https://gist.github.com/azell/b96d27e4091f5a966bae