closure-compiler
closure-compiler copied to clipboard
Array constructor with length is not eliminated
Consider the code:
/** @const {!Array<number>} */
const a = [1];
/** @const {!Array<number>} */
const b = Array(10);
b[0] = 2;
/** @const {!Array<number>} */
const c = [];
c.push(3);
/** @const {!Array<number>} */
const d = Array(10);
d.push(4);
When compiled with
yarn google-closure-compiler -O ADVANCED --js a.js --rewrite_polyfills=false
we get
Array(10)[0]=2;Array(10).push(4);
Arrays created with the Array(len) constructor are not eliminated.
google-closure-compiler version: v20230103
The peephole optimizations remove the cases of an array created with an array literal.
Presumably we could update whichever one does that so it also removes arrays created with the Array() constructor.
However, I don't think we're likely to prioritize fixing this. It's just not likely to be worth the effort.
I'll mark this as "help wanted", since I think it would be a good small contribution that could be made from outside our team.
Thank you! We'll look into fixing this
I want to work in this issue
Is it possible I work on this issue?
@brad4d Could you point to some relevant files in the code?
I would start by adding a test case here:
https://github.com/google/closure-compiler/blob/d77e3adfeee582d56c27420039f3ccb6c3e860ad/test/com/google/javascript/jscomp/PeepholeRemoveDeadCodeTest.java#L1476
fold("new Array(10);", "");
Then investigate how to make PeepholeRemoveDeadCode pass this test.