I just created two versions of a function that can be used to process the date and time and return it to different HTML elements. These elements will be displayed a phone lockscreen. The function is called every second, to make sure it jumps to the next minute in time.
- v.1 works at once: it processes every time unit (sec., min. day, month etc.) each second.
- v.2 works step by step: if the numbers of second is at any time “0” process the minutes. Then, if minutes is “0” process the hours etc.
I’d expected v.2 to be faster, but according to a test I did here: http://jsperf.com/timecalccompare it’s about 90% slower! Is it really slower or is the test unreliable?
I’d go with the simpler way, but additionally check if the minute has actually changed before updating the view and doing the rest of the calculations:
Also there’s absolutely no reason to declare the months and days array on each iteration, so: