Jump to content

Is it possible...


Drycodez
 Share

Recommended Posts

It is possible to create a Date object without assigning it to a variable. The following alerts the current year. All the parentheses are required:

alert( (new Date() ).getFullYear() );

Realistically, though, there is ALMOST NEVER not a reason to create a variable, or even several variables, in a situation like this. Code like ^^ is hard to read and gains you nothing.This next bit makes your intentions perfectly clear, and the additional overhead is so microscopically small that it will never be noticed.

var myDate = new Date();var myYear = myDate.getFullYear();alert(myYear);

There was a time when processors were small and RAM was expensive. It made sense to be stingy with characters and variables. Prgrammers even had contests to see who could write the shortest code to get the job done. The results were "cool" but generally unreadable. Those days are gone. The hardware and software that run your cellphone are more complex than the desktop PC I bought in 1988. Today's efficiency is all about humans and almost never about software (unless maybe you're programming a Mars Rover).

Edited by Deirdre's Dad
Link to comment
Share on other sites

... ...
alert( (new Date() ).getFullYear() );

Realistically, though, there is ALMOST NEVER not a reason to create a variable, or even several variables, in a situation like this. Code like ^^ is hard to read and gains you nothing.This next bit makes your intentions perfectly clear, and the additional overhead is so microscopically small that it will never be noticed.... ...

is it "nothing" or "microscopically small" ?and what negligible amounts are we talking here ?what additional processing time is taken when referencing a variable as opposed to a literal ?(let's say on an average PC of 'today' )
Link to comment
Share on other sites

Well, I benchmarked it. About 20 trials of 100,000 iterations each. I'm sitting at an old Mac G4 right now. The browser versions cannot be higher because the old chip won't run new system software. Actually, that's good for this sort of test. Worst case scenarios!The "short" version looks like this:

str = (new Date() ).getFullYear();

The "long" version:

D = new Date();Y = D.getFullYear();str =  Y;

FIREFOX 3.64There was one trial where the short version was slower than the long version by 2 milliseconds. All the other trials put the short version 20-100 milliseconds faster.SAFARI 4.0The short version usually won, but never by more than about 40 milliseconds. Sometimes it lost, but by a smaller margin. There were a lot of very close ties, 1-3 milliseconds difference.BUT REMEMBERI had to run the test 100,000 times even to see those differences on an 8-year-old computer. The maximum difference in efficiency to be inferred when the job needs to be done exactly ONCE? 100 millionths of a second. (Sorry if I'm off by a decimal place or so.)This is what I mean by microscopically small. Amounts of time that are so small they cannot be perceived by the human brain.Now, if you want to improve efficiency when you're animating a duck across the screen, that's a different story, requiring different techniques altogether.Did we learn something today? :)

Edited by Deirdre's Dad
Link to comment
Share on other sites

You're talking about nanoseconds or microseconds, and several bytes of memory.
ok, that's really insignificant !still, i believe it's important to know (and remember) First Principles.(i think one of you Angels mentioned your pet peeve with Dynamic Drive "off the shelf" scripts ?)
Well, I benchmarked it. ......BUT REMEMBERI had to run the test 100,000 times even to see those differences on an 8-year-old computer. The maximum difference in efficiency to be inferred when the job needs to be done exactly ONCE? 100 millionths of a second. (Sorry if I'm off by a decimal place or so.)This is what I mean by microscopically small. Amounts of time that are so small they cannot be perceived by the human brain.Now, if you want to improve efficiency when you're animating a duck across the screen, that's a different story, requiring different techniques altogether.
wow, now THAT'S a comprehensive reply !!MUCH appreciated !
Did we learn something today? :)
*always*Thanks, "Jim" :)
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...