Flip default parameter template (#4515)

* Flip default parameter template

YMMV, I saved ~10b on a 2kb library. Not noticeable at the small scale, by why not do it anyway?

I've (unscientifically) found that flipping the default parameter conditional yields better gzip results. I think this is due to the slightly longer string it can now repeatedly match:

```js
// old
var param = arguments.length <= 0 || void 0 === arguments[0] ? null : arguments[0]
--------------------------------------------------------------^

// new
var param = arguments.length > 1 && void 0 !== arguments[1] ? arguments[1] : null
------------------------------------------------------------------------^
```

Though it's entirely likely gzip will also choose up to the index of the arguments if you many default parameters at different indexes.

* Update tests
This commit is contained in:
Justin Ridgewell
2016-09-25 17:05:53 -04:00
committed by Logan Smyth
parent 08b45ca853
commit c2ed9de7fb
10 changed files with 31 additions and 31 deletions

View File

@@ -7,10 +7,10 @@ import * as t from "babel-types";
let buildDefaultParam = template(`
let VARIABLE_NAME =
ARGUMENTS.length <= ARGUMENT_KEY || ARGUMENTS[ARGUMENT_KEY] === undefined ?
DEFAULT_VALUE
ARGUMENTS.length > ARGUMENT_KEY && ARGUMENTS[ARGUMENT_KEY] !== undefined ?
ARGUMENTS[ARGUMENT_KEY]
:
ARGUMENTS[ARGUMENT_KEY];
DEFAULT_VALUE;
`);
let buildCutOff = template(`

View File

@@ -1,4 +1,4 @@
function foo() {
var a = arguments.length <= 0 || arguments[0] === undefined ? "foo" : arguments[0];
var a = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : "foo";
var b = arguments[1];
}
}

View File

@@ -1,11 +1,11 @@
var x = "outside";
function outer() {
var a = arguments.length <= 0 || arguments[0] === undefined ? function () {
var a = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : function () {
return eval("x");
} : arguments[0];
};
return function () {
var x = "inside";
return a();
}();
}
outer();
outer();

View File

@@ -1,12 +1,12 @@
var t = function () {
var e = arguments.length <= 0 || arguments[0] === undefined ? "foo" : arguments[0];
var f = arguments.length <= 1 || arguments[1] === undefined ? 5 : arguments[1];
var e = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : "foo";
var f = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 5;
return e + " bar " + f;
};
var a = function (e) {
var f = arguments.length <= 1 || arguments[1] === undefined ? 5 : arguments[1];
var f = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 5;
return e + " bar " + f;
};
};

View File

@@ -1,5 +1,5 @@
var t = function () {
var f = arguments.length <= 0 || arguments[0] === undefined ? "foo" : arguments[0];
var f = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : "foo";
return f + " bar";
};
};

View File

@@ -1,6 +1,6 @@
// #3861
function t() {
var x = arguments.length <= 0 || arguments[0] === undefined ? "default" : arguments[0];
var x = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : "default";
var _ref = arguments[1];
var a = _ref.a;
var b = _ref.b;
@@ -10,4 +10,4 @@ function t() {
}
console.log(x, a, b, args);
}
}