If I am going to be using Date() to generate dates and use its methods in my script should I create a Date() object once and re-use it or just create a new instance each time?
Is it more efficient\faster to do this:
var1 = new Date(2014, 5, 3)
var2 = new Date(2013, 2, 30)
var3 = new Date(2015, 10, 2)
or this:
myDate = new Date()
myDate.setDate(3)
myDate.setMonth(5)
myDate.setYear(2014)
var1 = myDate
myDate.setDate(30)
myDate.setMonth(2)
myDate.setYear(2013)
var2 = myDate
myDate.setDate(2)
myDate.setMonth(10)
myDate.setYear(2015)
var3 = myDate
Edit: my question wasn't very clear, let me try to explain:
I'm only using the Date() object for its methods and generating dates- meaning I don't really need a bunch of Date() objects just one I can use to do one-off operations like generate some date and call a method like toJSON() or something on it. After I generate the date and call the method I don't need a persistent object hanging around because I won't be doing anything with it. Would it consume less resources to reuse one Date() object like this or is it even slower?
Another edit:
I was thinking I could also put it in a function so I'd be doing this:
myDate = new Date();
setDate = function(dateObj,day,month,year) {
dateObj.setDate(dat);
dateObj.setMonth(month);
dateObj.setYear(year);
return myDate;
}
var1 = setDate(myDate,12,10,2014);
Anything value in doing it like this?