4

I've been a js developer for few years now. I've recently got into graphics / webgl programming and game engines etc. And no matter where I look, most code avoids the use of newer syntax such as () => {}, let, const etc. Sticking almost exclusively to var, function callbacks etc. Even Promises seem to be absent from a lot of the examples I've seen in favour of callbacks.

Examples cited such as all of the three.js docs, the babylon.js docs, the stack.gl examples, pixi.js, phaser and most game code out there seem to all use thisold style code. Now, admittedly some of these frameworks are firaly mature now, but examples and features have been added since the newer syntax has become more common place.

Is there a particular reason for this? There are a few potential reasons I can think of (although can't confirm):

  • Is var more heavily optimised in browsers? (I doubt it given that const comes with a degree of semantics as to it's usage and lifetime) and that arrow functions don't have a this context. Admittedly, not everyone can use newer language features, but for code readability these add benefit, no?
  • Does using newer syntax and transpiling to older style take a hit in terms of optimisation? I'm basing this on the assumption that you don't exert as much control over final output. However, I would assume that most transpilation engines such as Babel etc are built with this in mind? However, Is it worthwhile avoiding as much transpilation as possible for graphics / game code in general?
Lesbaa
  • 1,979
  • 2
  • 12
  • 15
  • I think most of these frameworks are simply older than ES6 (or at least, native ES6 support being wide-spread), so they started with the "old", then-normal way of doing it and found no good reason to update - never change a running system. – Bergi Apr 17 '18 at 18:05
  • 1
    I'm not sure this question is 100% answerable, unless industry giants from Khronos, toolkit authors, and major browser providers care to co-author an answer. That said, I absolutely believe it is because of legacy support. For example, IE11--which is still heavily used even today--can run WebGL, but does not support ES6. I'm sure there are other browsers and WebViews in a similar state. – TheJim01 Apr 17 '18 at 19:00
  • 1
    @TheJim01 supporting legacy browsers is definitely part of the reason. However, to be fair, OP does ask why they wouldn't just transpile down if that was the case. – 23k Apr 17 '18 at 22:45
  • 1
    @23k OP kind of answers their own question though by mentioning Babylon.js. Babylon.js isn't written in JavaScript, it's written in TypeScript and simply [targets es5](https://github.com/BabylonJS/Babylon.js/blob/6b3ae6225d10aca6b96ccb7544a40dd876282cb8/src/tsconfig.json#L5). One could pull the repo and change the target to es6 if they were so inclined. With respect to the examples, if I'm writing an _example_, I want it to run on as many supporting platforms as possible. It's all in personal preference, and an es6 example would be just fine (except in old browsers). – TheJim01 Apr 18 '18 at 00:28

0 Answers0