1

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");
BalaKrishnan웃
  • 4,337
  • 7
  • 30
  • 51
  • Maybe you should have a look at http://stackoverflow.com/questions/1056728/formatting-a-date-in-javascript to format your dates. – Chris Mar 14 '13 at 07:14
  • For starters you should try a real testcase where 10000 dates are formatted. You may want to check [a jsperf testsuite](http://jsperf.com/date-formatting-perf) instead. Although, you may consider also not converting 10000 dates at client-side – Alexander Mar 14 '13 at 07:32

1 Answers1

1

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());
Geuis
  • 41,122
  • 56
  • 157
  • 219