This one produces collision:
(() => {
const a = [];
for (let i = 0; i < 20_000; ++i) {
const pk = Date.now();
a.push(pk);
}
const uniqueItems = new Set(a);
console.log(a.length, uniqueItems.size); // 20000 3
console.log(a.length === uniqueItems.size); // false
})();
Is there a possibility of collision of primary keys even if I multiply the now()
(millisecond accurate) to 1,000?
(() => {
const a = [];
for (let i = 0; i < 20_000; ++i) {
const pk = Date.now() * 1_000 + i;
a.push(pk);
}
const uniqueItems = new Set(a);
console.log(a.length, uniqueItems.size); // 20000 20000
console.log(a.length === uniqueItems.size); true
})();
The timer-based primary key will be used for syncing data to multiple devices to ensure efficiency. Only the data from the last synced time and forward will be pulled from the server on the succeeding syncs.