Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Will new Date().toJSON() always be unique in Javascript?

The PouchDB Manual suggests using Date().toJSON() to generate a new id for each document. Do all javascript runtimes guarantee that Date().toJSON() will always be unique?

like image 392
Ole Avatar asked Feb 18 '26 10:02

Ole


1 Answers

The dates only have microsecond precision, so there's no guarantee they will be unique.

The snippet below will give you a number of duplicates in all but the slowest runtime environments:

for (let i = 0; i < 10; i++) {
  console.log(new Date().toJSON())
}
like image 117
Robby Cornelissen Avatar answered Feb 20 '26 04:02

Robby Cornelissen