Transpiled For-Of Loops Are Bad for the Client
Go one step further, eliminate the polyfill, and just write a traditional for loop.<p>Several years ago, I decided to try out the then-newish E4X for-each syntax.  It was only implemented in SpiderMonkey, but that was okay because not only am I not a web developer, this project wasn&#x27;t meant to run on the web, either—it just happened to be in JS.  I knew for a fact that this code wouldn&#x27;t be running anywhere except SpiderMonkey, so I dug in.  After some research, though, I discovered some problem.  Some subtlety that made it a non-starter.  So I kept writing traditional for loops.<p>A couple years later, I was starting work on a kind-of-new codebase; it was essentially greenfield, but I knew I was going to end up cribbing a bunch from an existing codebase.  When my fingers began typing out the familiar `for (`, I paused.  This would be the first for loop introduced in the project, and I should maybe make a decision to get things right from the beginning.  By now, for-of had arrived, and it was being standardized in ES<i>x</i> proper.  Should I maybe do another survey of the lands and look into giving the new iteration syntax a shot?  It was a simple iteration: a straight pass through a list of items, one after another.  And in addition to being able to address items by index, they all had the equivalent of a &quot;next&quot; pointer for the item that followed.  So I deferred; I went with a while loop instead, chasing &quot;next&quot; pointers to avoid having to do some in-depth research on the then-current state of `for`.  I got pretty far doing this, actually, and was able to go the next few days writing all my loops in this way, just to avoid tacitly endorsing either for-of or traditional for loops.<p>After several days, however, that ran out, and I came to point where I would not be able to sidestep it and needed to make a decision.  So I stopped, and dug in again.  Another problem surfaced: for-of was only implemented in the newest releases of SpiderMonkey, and the only enhanced iteration syntax available in previous releases that this code would need to run on was the now-deprecated for-each.  I thought really hard about this.  Was there some part of the language that could be exploited to find some clever way around this?  No.  Maybe consider a transpiler?  After all, for-of would be standard, so <i>eventually</i> support is going to be widespread enough that even the lowest common denominator will have native support...<p>But then I stopped.  Because the whole thing is stupid.  Just write a damn for loop.  What was the real problem?  Trying to save time?  Except how much time was already sunk into trying to shave off, what?  Like <i>one second</i> of typing?  Maybe two?  And the tradeoff for that development time gain is (a) a runtime performance penalty; (b) having to either throw everything older than version <i>x</i> by the wayside or spend all those extra seconds and more to put some transpiler crud in place; and (c) code that&#x27;s harder to understand for anyone who doesn&#x27;t fetishize staying on top of the newest language features.  The latter also involves people who typically work in other languages and would probably immediately understand `i = 0; i &lt; n; i++` (even if it&#x27;s not idiomatic for their preferred ecosystem), but likely wouldn&#x27;t immediately grasp the nuances of the new iteration protocol.<p>What&#x27;s wrong with `i = 0; i &lt; n; i++`, anyway?  It&#x27;s not even that verbose.  What it is, is predictable.  Which means your eyes get used to it, just like any other pattern.  Except traditional for loops are powerful, too, because you might want to do something, say, for every fourth item.  Or maybe skip the first.  Or iterate backwards.  And you can.  In each case, anything not fitting the traditional pattern will stick out.<p>Anyway, I still write traditional for loops today.  I don&#x27;t feel like I&#x27;m missing out on anything, and my thoughts very much echo the sentiments in an essay published a few years ago on a similar topic: &quot;Java for Everything&quot;.<p><a href="https:&#x2F;&#x2F;www.teamten.com&#x2F;lawrence&#x2F;writings&#x2F;java-for-everything.html" rel="nofollow">https:&#x2F;&#x2F;www.teamten.com&#x2F;lawrence&#x2F;writings&#x2F;java-for-everythin...</a>