babel / babel/minify

Sub-optimal boolean returns

Open
#29 5 comments 0 reactions 0 assignees View on GitHub
enhancement
Dominant language
JavaScript
Stars
4.4k
Forks
217
PR merge metrics
No merged PRs in 30d

Description

Input:

``` js
function isTextInputElement(elem) {
if (!elem) {
return false;
}

if (elem.nodeName === 'INPUT') {
return !!supportedInputTypes[elem.type];
}

if (elem.nodeName === 'TEXTAREA') {
return true;
}

return false;
}
```

GCC Output:

``` js
function(a){return a?"INPUT"===a.nodeName?!!supportedInputTypes[a.type]:"TEXTAREA"===a.nodeName?!0:!1:!1}
```

Two things to be noted:
- `if (a === b) { return true; } return false;` is outputted as `a === b ? !0 : !1` which can be simplified as `a === b`, no need to do the condition.
- all the return values have ! in common, it is possible to factor it out. `return !(a?"INPUT"===a.nodeName?!supportedInputTypes[a.type]:"TEXTAREA"===a.nodeName?0:1:1)`

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.