Jump to content

Function Declarations


GerryH

Recommended Posts

First, I have to commend all you folks that "give-back" your expert knowledge for free, a big thank you! :) My question is, you can declare a function 2 ways,

function myfunction1(myparams...){   ...}

or;

var myfunction2 = function(myparams...){   ...}

Is there any performance advantage to using one style over the other, or is it just symantics?And while I got your attention, quicky about global variables. As a C programmer, I know these of course are the evil of all mothers, but there are times when they are necessary. I understand the global namespace is finite, is there a way to determine the available space per client and how this would effect overall browser performance? (I couldn't find any results in a search for any such benchmark tests)Thanks again,Gerry

Link to comment
Share on other sites

The main difference is actually scoping. If you define several functions the second way, they're only going to be available in the scope that they're defined in, so you could be more efficient with memory if you namespace the functions and then delete anything you don't need after you're finished with it (or just let it go out of scope and get cleaned up).For the second question, it's not possible to figure out how much memory a browser has available to use.

Link to comment
Share on other sites

Thanks for the quick response!The scope makes sense (heck never even thought of creating a "throw away" function, but that's good to know), I made the post because I've seen examples of it used in the 2nd format declared globally, which made me curious. As far as the memory query, I guess we can't really get much info (at this time) about the clients config other than navigation info, or what screen res their running. The reason I even brought this up was performance dissimilarities using a very simple javascript to fade an elements opacity as the "setTimeout()" performs very differently based on CPU and RAM. I understand the "interval" parameter is the minimum before it executes it's function again, but as an example, running this simple script on a P4 3.2/3G RAM runs this script in 750-900ms vs a P4 1.4/512 which yeilds between 3000-5000ms.

// Note: interval is a global var set at 1function fadeOut(id){	var x = document.getElementById(id);	time -= 4; // was 6	fftime = fftime - 0.051;	if (browserType == 'FF')		x.style.opacity=fftime;	else			x.style.filter = 'alpha(opacity = ' + (time) + ')';	 t = setTimeout(function(){fadeOut(id)},interval);	if (browserType == 'IE' && time <= 0 || browserType == 'FF' && fftime <=0)	{		if (browserType == 'FF')	x.style.opacity=0.0;		else					x.filters.alpha.opacity=0;					clearTimeout(t);		fadeIn(id);			}}

Any suggestions for this idiot! ;PThanks

Link to comment
Share on other sites

Using setInterval will give you more accurate results, as that doesn't "wait" until one execution is over before beginning the countdown to the next one.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...