Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Quick question on performance

Status
Not open for further replies.

vladibo

Programmer
Sep 14, 2003
161
CA
What is more efficient:

A. if (!setWidth) { DoWork() }

or

B. if (typeof setWidth == 'undefined') { DoWork() }
 
if (!setWidth)

is NOT a check to see if setWidth is defined or not. If setWidth is zero, it will pass the conditional test you have. The 2 examples you have will not provide the same results, which makes your question moot.

Lee
 
Besides which... running that kind of test produces javascript errors. I'd put it within an try/catch but that defeats the purpose of any testing. And yeah... the first one fails if the object is zero.

[smile]

Jeff

[tt]Jeff's Page [/tt][tt]@[/tt][tt] Code Couch
[/tt]
 
In my case the object is always of type Function so my question is actually: Does anybody know what operations are performed on a lower level on (!Variable)

My code looks like:

if (!setWidth) { setWidth = new Function("return something;") }
 
I usually use 2 dates to test the processing time of a certain block of code. Define one Date before and one after the code and substract the 2nd from the first. An easy way to see how long it took to process the code.
 
I mean if on the C++ level the operations for (!variabale) are

1. Retrieve the variable type as string
2. Check if is null
3. Compare this string with 'undefined'
4. Check if is 0

Then if (typeof setWidth == 'undefined') { DoWork() } might be even more efficient

But if it is

1. Check how many bit are associated with the space allocated for this variable

then (!setWidth) is better





 
This is a way to check if a function is defined and if not to define it
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top