evanw / evanw/esbuild

Missed minimization opportunity

Open
#571 4 comments 0 reactions 0 assignees View on GitHub
suboptimal-output
Dominant language
Go
Stars
40.1k
Forks
1.3k
PR merge metrics
No merged PRs in 30d

Description

Hi,

First of all, thanks for esbuild. I'm using it in a polyfill generation service I'm writing and it's awesome :)
I noticed there are some polyfills that terser can minimize better. For example, for the following code:

```
// https://developer.mozilla.org/en-US/docs/Web/API/ChildNode/remove
(function (arr) {
arr.forEach(function (item) {
if (item.hasOwnProperty('remove')) {
return;
}
Object.defineProperty(item, 'remove', {
configurable: true,
enumerable: true,
writable: true,
value: function remove() {
this.parentNode.removeChild(this);
}
});
});
})([Element.prototype, CharacterData.prototype, DocumentType.prototype]);
```

terser is able to elide the function call by doing the following:
```
[Element.prototype,CharacterData.prototype,DocumentType.prototype].forEach((function(e){e.hasOwnProperty(\"remove\")||Object.defineProperty(e,\"remove\",{t:!0,i:!0,o:!0,value:function(){this.parentNode.removeChild(this)}})}));
```

esbuild keeps the function call:
```
(function(r){r.forEach(function(e){if(e.hasOwnProperty(\"remove\"))return;Object.defineProperty(e,\"remove\",{configurable:!0,enumerable:!0,writable:!0,value:function(){this.parentNode.removeChild(this)}})})})([Element.prototype,CharacterData.prototype,DocumentType.prototype]);
```

Any interest in adding this sort of optimization? I could also take a stab at implementing it if you can point me to a good spot to look at.

Contributor guide

No contributing guide indexed for this repository

Research direction

Use the supplied polyfill as a reproduction and compare esbuild's output with terser's output. No source file or test entry point is named, so first locate the JavaScript minification optimization code and its existing tests. Done means esbuild produces the smaller equivalent output without changing behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
compilers
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.