Getting unique timestamps in Javascript

Sometimes, when you want to generate unique id’s or similar, it is very common to use the current date as unix timestamp (new Date().getTime() or $time() in Mootools). The problem is that if we get the current timestamp multiple times too fast, it may happen that we get exactly the same value. Actually, this will happen very easily, as this tests shows:

var times = [];
while (times.length < 20) {
    times.push(new Date().getTime());
}
console.log(times);
// All timestamps returned would be pretty much the same

To prevent this, we can use this little function which ensures we get a unique timestamp every time we call it:

// Mootools
function getUniqueTime() {
  var time = $time();
  while (time == $time());
  return $time();
}
// Javascript generic
function getUniqueTime() {
  var time = new Date().getTime();
  while (time == new Date().getTime());
  return new Date().getTime();
}

Test it in Firebug:

function getUniqueTime() {
  var time = new Date().getTime();
  while (time == new Date().getTime());
  return new Date().getTime();
}
var times = [];
while (times.length < 20) {
  times.push(getUniqueTime());
}
console.log(times);
// Now all timestamps are unique

Nice, uh?

3 thoughts on “Getting unique timestamps in Javascript

  1. Ihor

    Sorry, but that is bad:

    try to run next:
    var getUniqueTimestamp = function() {

     var time = new Date().getTime();

     while (time == new Date().getTime());

     return "q" + new Date().getTime();

    }
    var getUniqueTimestampRandom = function() {

     return "q" + new Date().getTime() + Math.random;

    }

    var start = new Date().getTime() ;
    for (var i = 0; i < 10000; i++)
          getUniqueTimestamp();

    var time = new Date().getTime() - start;
    console.log( time);

    var start = new Date().getTime() ;
    for (var i = 0; i < 10000; i++)
          getUniqueTimestampRandom();

    var time = new Date().getTime() - start;
    console.log( time);

    results for me:
    10371 // and your example take 100% of CPU
    53

    because you function takes 100% CPU for 1 ms each time. so can’t be less then N * 1 ms. — 10 sec for 10000 times.

    please use timestamp + random to generate unique ID.

  2. Eneko Alonso Post author

    Definitely, not the best solution if you want to generate multiple ids per second :) This post is quite old and as far as I remember, we were looking for actual timestamps without repeating them, so waiting one second to get the next was ok.

    The code in the post was never intended to generate multiple ids in a short time, because it would be a really bad solution for that. It was intended to prevent rare collisions when two ids where requested within the same second.

    Anyhow, thanks for your comment!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Additional comments powered by BackType