0

I should mention that this will be going into a database so it truly needs to be unique. I need to define the id before it enters the database no questions asked.

for (var i = 0; i < obj.length; i++) {
    var id = Date.now();
    console.log(id);
}

The problem is, this is the output:

1428356251606
1428356251607
1428356251607
1428356251607
1428356251607
1428356251607
1428356251607
1428356251607
1428356251607
1428356251607
1428356251607
1428356251607
1428356251607
1428356251607
1428356251607
1428356251607
1428356251607
1428356251607
1428356251607
1428356251607
1428356251607
1428356251607
...

I was thinking of using Math.random() but it might in the name of all the Norse gods actually hit the same number twice.

Any idea of how to make this truly unique while sustaining the speed of the for loop?

basickarl
  • 37,187
  • 64
  • 214
  • 335

3 Answers3

4

You should use i. i is guaranteed to be unique for every iteration of your loop.

Depending on how long lasting you want the uniqueness of your ID to be, you can add another unique compound to the ID (Date.now() is a good candidate, because it's guaranteed to be unique across different runs on the same machine at different times).

Madara's Ghost
  • 172,118
  • 50
  • 264
  • 308
  • 1
    Pending situation, this works. If it's like a database unique ID, maybe a UUID would be better? – Sterling Archer Apr 06 '15 at 21:45
  • @SterlingArcher you cannot tell if something is "better" or "worse" than the other unless you have constraints and comparison criteria. For the question as it is asked - using `i` is the "ideal" solution. – zerkms Apr 06 '15 at 21:46
  • "across different runs" --- *non-concurrent* runs – zerkms Apr 06 '15 at 21:53
  • "i is guaranteed to be unique for every iteration of your loop." - `var obj = {length: Number.MAX_SAFE_INTEGER + 2}` PROBLEM? – Benjamin Gruenbaum Apr 06 '15 at 21:54
  • @SterlingArcher Is in on the correct path, it will be stored in a database, however Second Rikudo you did answer the question specifically so you get a cookie. – basickarl Apr 06 '15 at 21:54
  • @KarlMorrison then it should be database that acts as a source of truth, not JS. – zerkms Apr 06 '15 at 21:54
  • @KarlMorrison what database if I may ask? The solution is usually to have the database assign these IDs and not the application code. – Benjamin Gruenbaum Apr 06 '15 at 21:55
  • 1
    @KarlMorrison If it's going to be stored in a database, why do you not let the database assign the ID? MySQL has `AUTO_INCREMENT`, Postgres has `SERIAL`, Mongo has automatic ID assignmnent. – Madara's Ghost Apr 06 '15 at 21:55
  • @zerkms I wish it were so easy, I require the unique id as the script is involved in clustering data, I need to sort it out and locate some of the data in the clusters before putting it into the database, hence the unique id. – basickarl Apr 06 '15 at 21:59
  • Thanks Second Rikudo, the `Date.now()` + `i` will work! – basickarl Apr 06 '15 at 22:06
  • 1
    @KarlMorrison Some databases can handle clustering for you. Have a look at Apache Cassandra. – Madara's Ghost Apr 06 '15 at 22:34
  • @SecondRikudo Thank you! Looks interesting indeed. – basickarl Apr 07 '15 at 13:29
2

How about

var id = Date.now();
for (var i = 0; i < obj.length; i++) {
    console.log(id+i);
}
Gabriele Petrioli
  • 191,379
  • 34
  • 261
  • 317
-1

You can force iterate until the ids are repeating

var lastGenerated = 0;
var id = 0;
for (var i = 0; i < obj.length; i++) {
    do{
       id = Date.now();
    }while(lastGenerated == id);
    lastGenerated = id;
    console.log(id);
}

or you can make like bellow

var seed = Date.now() * 1000;
for (var i = 0; i < obj.length; i++) {
    var id = seed + i;
    console.log(id);
}

A good solution is create a object to manager the id's ...

(function(w){

   var __id = Date.now();

   function ID(){   }

   ID.prototype.next = function(){

       return ++__id;

   };

   w.ID = new ID();

}(window));





for(var i = 0; i < obj.length; i++){

    var id = ID.next();
    console.log(id);

}
Luan Castro
  • 1,184
  • 7
  • 14