What is best approach out of below options:
option 1:
var d = new Date();
uniqueString = d.getTime();
option 2:
uniqueString = Math.random();
What is best approach out of below options:
option 1:
var d = new Date();
uniqueString = d.getTime();
option 2:
uniqueString = Math.random();
It is possible (however unlikely) that by using dates (sequential, not random) that two different instances could coincide.
The odds of an overlap from Math.random() are much lower (again possible, however unlikely).
Out of the two I would go for the second option.
While getTime()
would yield 13 digits, most of them being constant in a period of weeks, random()
would randomize a number with about 16 digits.
Note that if by numeric you mean digits only, then you would have to work a little more to get rid of the 0.
part of the randomized number.