Reputation: 4557
I am using this code to convert date object to date string.
var startDate = dateObject;
var dateString = startDate.getMonth() + 1 + "/" + startDate.getDate() + "/" + startDate.getFullYear(); // to display in "M/d/yyyy" format
what happens is it take 0.003 sec in IE 10, I am converting more then 10000 dates, it affects the overall performance of my app. Is there is any way to improve the performance?
I am using this code to check performance.
var d = new Date();
var startDate = dateObject;
var dateString = startDate.getMonth() + 1 + "/" + startDate.getDate() + "/" + startDate.getFullYear();
$startTimeCol.html(dateString);
var ticks = ((new Date() - d) / 1000);
console.log("toString: " + ticks + "sec");
Upvotes: 1
Views: 147
Reputation: 42277
Ok, this took some time to put together and test.
You want most likely want to use String().concat. View the comparison code here, http://jsfiddle.net/VbCyP/1/
I compare 3 versions: String concatenation with +, string concatenation with an array and join, and String.concat.
In testing the latest versions of Chrome, Safari, and Firefox on a 2009 Macbook, String().concat is consistently the fastest for this operation.
Example code from the jsfiddle:
var x = String().concat(dates[i].getMonth() + 1, '/', dates[i].getDate(), '/', dates[i].getFullYear());
Upvotes: 1