haxe icon indicating copy to clipboard operation
haxe copied to clipboard

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

Open AxGord opened this issue 1 year ago • 0 comments

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);

AxGord avatar Jun 08 '24 18:06 AxGord