HaxeFoundation / haxe

Haxe - The Cross-Platform Toolkit
https://haxe.org
6.03k stars 648 forks source link

[optimisation] For loop is not removed if variable is used #11691

Open AxGord opened 3 weeks ago

AxGord commented 3 weeks ago

For loop is not removed if variable is used. In cases where inline methods are used, an array is created in the same way, although it is possible without it.

var m:Float = 0;
for (e in [1, 2, 3]) m = Math.max(m, e);
trace(m);

Compiles to js:

let m = 0;
m = Math.max(m,1);
m = Math.max(m,2);
m = Math.max(m,3);
console.log("Test.hx:7:",m);

But

var m:Float = 0;
var a = [1, 2, 3];
for (e in a) m = Math.max(m, e);
trace(m);

Compiles to js:

let m1 = 0;
let a = [1,2,3];
let _g = 0;
while(_g < a.length) m1 = Math.max(m1,a[_g++]);
console.log("Test.hx:12:",m1);