Re: looking for a javascript calendar...
Dr John Stockton wrote:
[...][color=blue]
>
> So October has 30 days in .auau ?[/color]
I think it's pronounced "Ow Ow". Thanks.
[color=blue]
>
> After if (...) return there is no need for an else.
>
> No need to test M==2 more than once.
>
> function getMonthDays(Y, M) {
> if ( M==4 || M==6 || M==9 || M==11) return 30
> if ( M==2 ) return 28 + (Y%4==0 && ( Y%100!=0 || Y%400==0))
> return 31 }[/color]
I think this one is easiest to understand, but...
[color=blue]
>
> function getMonthDays(Y, M) {
> return M==4 || M==6 || M==9 || M==11 ? 30 :
> M==2 ? 28 + (Y%4==0 && ( Y%100!=0 || Y%400==0)) : 31 }[/color]
this one has the best trade-off between simplicity and clarity.
[...][color=blue]
> The first two make a redundant test for non-centurial leap years. The
> rest would be efficient if compiled, but maybe not so when interpreted.
>[/color]
These may have their good points in terms of exploitation of
JavaScript features, but testing showed them all to take almost
exactly the same amount of time.
I had to calculate the number of days in each month of they year
for 20,000 years to get reasonable numbers (about 400ms), so I
don't think optimisation for speed is required here.
I tried optimising by first testing for months with 31 days:
return M==1 || ... M==12? 30:
but it was 10% slower - I guess all those ORs are slow.
--
Rob
Dr John Stockton wrote:
[...][color=blue]
>
> So October has 30 days in .auau ?[/color]
I think it's pronounced "Ow Ow". Thanks.
[color=blue]
>
> After if (...) return there is no need for an else.
>
> No need to test M==2 more than once.
>
> function getMonthDays(Y, M) {
> if ( M==4 || M==6 || M==9 || M==11) return 30
> if ( M==2 ) return 28 + (Y%4==0 && ( Y%100!=0 || Y%400==0))
> return 31 }[/color]
I think this one is easiest to understand, but...
[color=blue]
>
> function getMonthDays(Y, M) {
> return M==4 || M==6 || M==9 || M==11 ? 30 :
> M==2 ? 28 + (Y%4==0 && ( Y%100!=0 || Y%400==0)) : 31 }[/color]
this one has the best trade-off between simplicity and clarity.
[...][color=blue]
> The first two make a redundant test for non-centurial leap years. The
> rest would be efficient if compiled, but maybe not so when interpreted.
>[/color]
These may have their good points in terms of exploitation of
JavaScript features, but testing showed them all to take almost
exactly the same amount of time.
I had to calculate the number of days in each month of they year
for 20,000 years to get reasonable numbers (about 400ms), so I
don't think optimisation for speed is required here.
I tried optimising by first testing for months with 31 days:
return M==1 || ... M==12? 30:
but it was 10% slower - I guess all those ORs are slow.
--
Rob
Comment