# Please improve gen js

**URL:** <https://community.haxe.org/t/please-improve-gen-js/2309>\
**Category:** Haxe\
**Created:** [March 14, 2020, 5:50am UTC](https://community.haxe.org/t/please-improve-gen-js/2309 "2020-03-14T05:50:20Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Zhan](https://community.haxe.org/user_avatar/community.haxe.org/zhan/32/468_2.png) [@Zhan](https://community.haxe.org/u/Zhan)\
**Post date:** [March 14, 2020, 5:50am UTC](https://community.haxe.org/t/please-improve-gen-js/2309/1 "2020-03-14T05:50:20Z")

</div>

![image](https://community.haxe.org/uploads/default/original/2X/9/92256f91235efe726b3a03bb8cd85ef355ab8811.png)

---

<div class="post-metadata">

**Author:** ![RealyUniqueName](https://community.haxe.org/user_avatar/community.haxe.org/realyuniquename/32/10_2.png) [@RealyUniqueName](https://community.haxe.org/u/RealyUniqueName)\
**Post date:** [March 14, 2020, 7:43am UTC](https://community.haxe.org/t/please-improve-gen-js/2309/2 "2020-03-14T07:43:51Z")

</div>

You can disable loop unrolling with `-D loop_unroll_max_cost=0`

---

<div class="post-metadata">

**Author:** ![Zhan](https://community.haxe.org/user_avatar/community.haxe.org/zhan/32/468_2.png) [@Zhan](https://community.haxe.org/u/Zhan)\
**Post date:** [March 14, 2020, 7:44am UTC](https://community.haxe.org/t/please-improve-gen-js/2309/3 "2020-03-14T07:44:48Z")

</div>

> [@RealyUniqueName](#):
>
> `-D loop_unroll_max_cost=0`

why not make this options as default?

because this -D XXXX have no where to find documents .

---

<div class="post-metadata">

**Author:** ![kevinresol](https://community.haxe.org/user_avatar/community.haxe.org/kevinresol/32/272_2.png) [@kevinresol](https://community.haxe.org/u/kevinresol)\
**Post date:** [March 14, 2020, 9:49am UTC](https://community.haxe.org/t/please-improve-gen-js/2309/4 "2020-03-14T09:49:17Z")

</div>

I think unrolling is actually considered as an “improvement” (towards performance)

---

<div class="post-metadata">

**Author:** ![Zhan](https://community.haxe.org/user_avatar/community.haxe.org/zhan/32/468_2.png) [@Zhan](https://community.haxe.org/u/Zhan)\
**Post date:** [March 14, 2020, 9:51am UTC](https://community.haxe.org/t/please-improve-gen-js/2309/5 "2020-03-14T09:51:03Z")

</div>

I don’t think it’s good idea to unroll,it’s make codes too big.

---

<div class="post-metadata">

**Author:** ![ibilon](https://community.haxe.org/user_avatar/community.haxe.org/ibilon/32/13_2.png) [@ibilon](https://community.haxe.org/u/ibilon)\
**Post date:** [March 14, 2020, 9:52am UTC](https://community.haxe.org/t/please-improve-gen-js/2309/6 "2020-03-14T09:52:51Z")

</div>

You can find it in `haxe --help-defines`

```haxe
loop-unroll-max-cost : <cost> Maximum cost (number of expressions *
                             iterations) before loop unrolling is canceled.
                             (default: 250)

```

Help defines being mentioned in `haxe --help`.

And yes loop unrolling helps performances, sure the code is less pretty, but the js interpreter doesn’t care 😉

---

<div class="post-metadata">

**Author:** ![RedHeadEatBread](https://community.haxe.org/user_avatar/community.haxe.org/redheadeatbread/32/894_2.png) [@RedHeadEatBread](https://community.haxe.org/u/RedHeadEatBread)\
**Post date:** [March 14, 2020, 10:07am UTC](https://community.haxe.org/t/please-improve-gen-js/2309/7 "2020-03-14T10:07:50Z")

</div>

Information on compiler flags is also in the manual [Global Compiler Flags - Haxe - The Cross-platform Toolkit](https://haxe.org/manual/compiler-usage-flags.html)

You can also use a variable instead of a hard-coded value

```haxe
var x = 6;
for (i in 0...x) ...

```

---

<div class="post-metadata">

**Author:** ![RobDangerous](https://community.haxe.org/user_avatar/community.haxe.org/robdangerous/32/55_2.png) [@RobDangerous](https://community.haxe.org/u/RobDangerous)\
**Post date:** [March 16, 2020, 12:08am UTC](https://community.haxe.org/t/please-improve-gen-js/2309/8 "2020-03-16T00:08:48Z")

</div>

I did a quick search on what the current state of JS engines is in regards to loop unrolling. According to [1039458 - Unroll tight loops](https://bugzilla.mozilla.org/show_bug.cgi?id=1039458) Firefox can in theory do it itself but it’s not activated by default. Didn’t find anything about V8 but according to [performance - JavaScript - Are loops faster than discretely writing line-by-line? - Stack Overflow](https://stackoverflow.com/questions/38111355/javascript-are-loops-faster-than-discretely-writing-line-by-line) it at least didn’t yet happen mid 2016.  
Looks like it does indeed still make sense to unroll loops for JS.  
Does not seem to make sense for C++ at all though, this basically just hides information from clang/gcc/msvc which all can do loop unrolling themselves and can combine it with other optimizations in fancy ways. Probably the same for Java and C# but I don’t know for sure and I don’t care enough for it to have a closer look.

PS: Please don’t use a variable for your loop bounds - if anything it’ll just make things slower.

---

<div class="post-metadata">

**Author:** ![Simn](https://community.haxe.org/user_avatar/community.haxe.org/simn/32/388_2.png) [@Simn](https://community.haxe.org/u/Simn)\
**Post date:** [March 16, 2020, 8:14am UTC](https://community.haxe.org/t/please-improve-gen-js/2309/9 "2020-03-16T08:14:42Z")

</div>

It depends. For instance, it’s probably possible to construct cases where the constant index makes it possible for a closure call to be inlined. Some optimizations like that are only possible at higher-level.

Whether or not that actually happens in real code is another question though. We could indeed consider having this optimization off by default on targets that have good native (JIT) compilers.

---

<div class="post-metadata">

**Author:** ![RobDangerous](https://community.haxe.org/user_avatar/community.haxe.org/robdangerous/32/55_2.png) [@RobDangerous](https://community.haxe.org/u/RobDangerous)\
**Post date:** [March 16, 2020, 5:30pm UTC](https://community.haxe.org/t/please-improve-gen-js/2309/10 "2020-03-16T17:30:49Z")

</div>

Interesting, I thought about whether unrolling would allow further optimizations like that but couldn’t come up with any. Thanks!
