This project is read-only.

parameter defaults - generated undefined checks

Topics: General
Feb 7, 2013 at 4:33 PM
The code for parameters with default values is generated as:

if (typeof paramName === "undefined") { paramName = defaultValue; }

Why is typeof being used. Why not just compare to undefined directly?

if (paramName === undefined) { paramName = defaultValue; }
Feb 7, 2013 at 6:25 PM
Edited Feb 7, 2013 at 6:33 PM
It's not perfect.
function foo(bar=10) {


foo(); // bar equals 10
foo(undefined); //Oops: bar = 10
A better way:
function foo(bar=10) {
    if(arguments.length !== 0){
        bar = 10;   
Feb 7, 2013 at 8:08 PM
The test result is true in both cases you mention and thus bar would equal 10 in both cases. See:
Feb 7, 2013 at 8:59 PM
Edited Feb 7, 2013 at 9:01 PM
'typeof' is much faster than === undefined in IE, and of similar performance in Chrome (theoretically, the 'typeof' check should more optimizable because you don't have to look up the value of the symbol 'undefined'). It's also safer in the admittedly insane case where someone has mutated the value 'undefined'.

Checking argument length is very, very slow, and doesn't let you do things like specify the 3rd argument while omitting the 2nd.

Feb 7, 2013 at 9:16 PM
Actually, typeof is not the fastest in my IE.
Feb 8, 2013 at 9:01 AM
Updated the results for FireFox and Opera.

I personally think we can live with the fact that foo(undefined); // bar = 10

@rcollette, your test appears to be IE9.

But both FireFox and Opera favour if-Undefined - albeit by smaller margins than IE10's margin on 'if-Typeof-Undefined.
Apr 12, 2013 at 11:50 AM
nabog is correct - I've run your test on IE, Chrome and Firefox, and if-undefined is faster on Chrome by 10%, on Firefox by 24%. If-typeof-undefined is faster only on IE, where if-undefined is 52% slower.

Since you're affiliated with Microsoft, I suspect you'll want to optimize for IE in the default case, however, I'd appreciate a compiler switch for generating if-undefined instead of if-typeof-undefined code for use with other browsers.

And of course, TypeScript being open source, I could try doing that myself, but I think it'll be easier for you to get the change out if you add the switch yourself. Shouldn't be too difficult?
Apr 12, 2013 at 11:53 AM
..Of course, thinking this thing over for a while, the differences are small enough not to warrant the extra work, since performance-critical code shouldn't use optional parameters anyway, and for the rest.. let's just say, that if you're running enough optional parameter logic for it to make a significant difference in performance, you're doing something wrong.
Sep 3, 2013 at 3:14 PM
If you pass in null the default parameter does not get assigned.
//Assign null from another method.
this._direction = null;

// The transitionToNextView method gets called and since this._direction is null the default value does not get assigned.
this._transitionManager.transitionToNextView(view, this._direction);

// The method. I would assume undefined and null would set transitionType to TransitionType.NONE but the value is null.
public transitionToNextView(nextView, transitionType:string = TransitionType.NONE):any {
null: A value has not been assigned or it was nulled out.
undefined: The property does not exist.