6 ms·
Looks like the JavaScriptCore people are on it :) As an aside, I’ve been seeing a number of articles show up that basically go like “Chrome implements x effici
by saagarjha 6y ago
Looks like the JavaScriptCore people are on it :)
As an aside, I’ve been seeing a number of articles show up that basically go like “Chrome implements x efficiently but semantically equivalent y poorly, you should use always use x”. I really hope we don’t end up with JavaScript code being written to be optimized well on one particular implementation of a JavaScript engine…it’s interesting to see this in this area specifically because other VMs (e.g. Java) have knobs you can turn yourself but with JavaScript your code runs without much input on your side, so methods to influence the VM are much more specific…
- yladiz 6y agoThis has been happening for a long time, sadly. Years ago Chrome had poor optimization around try-catch and this meant it couldn't be used in the application I was working on, as we pretty much targeted only Chrome. I guess by the time async-await got into the engine try-catch was optimized enough and now this doesn't seem to be an issue. Hopefully this changes, even slowly, but it'll probably take a while since Chrome's got so much share.
- smt88 6y agoJS is so ultra-optimized by modern browsers that we're going to run into cases like this. It's inevitable. Hopefully they're always treated like bugs and not something we need to work around.
- olliej 6y agoThe JSC team (and I suspect V8 and SM) always treat performance cliffs caused by normal/sensible code patterns as a bug. The problem is that when implementing improvements it's easy to forget every mostly-equivalent thing and so miss obvious things (often the entire fix in the end is "if (canDoItFast) doTheExistingFastThing()"
- smt88 6y agoI can imagine some workarounds for that. Go to GitHub and pull the tests for a few thousand libraries, and watch for major performance changes. I mean I'm sure they have a solution, since this seems like a rare occurrence. I will note though that const/let are not mostly equivalent to var. They're often used interchangeably, but they work pretty differently as far as a JS engine is concerned.
- nostrademons 6y agoI remember doing this back in 2006 before Chrome even came out. Remember when it was a good idea to cache the .length property because it was O(N) on some browser (I think an early IE), so you'd write your for-loops like this? for (var i = 0, len = arr.length; i < len; ++i) { ... } Hell, the whole premise for React (the virtual DOM) is based on outdated performance advice. Chrome has used a dirty-bit for DOM manipulations since a year or two after React came out; manipulating the DOM is within a factor of ~2-4 of setting a property on a JS object (and much faster than constructing & copying whole new JS objects, which incurs a GC cost), it's just that you really want to avoid interspersed manipulations & queries, which force a page reflow: parent.appendChild(document.createElement('div')); // Fast; ~50 us let w = parent.innerWidth; // Slow, forces reflow; ~20 ms let h = parent.innerHeight; // Fast again; no page modifications parent.appendChild(document.createElement('div')); // Fast; just sets dirty bit parent.appendChild(document.createElement('div')); // Still fast; dirty bit already set But that's the nature of a lot of technical rules of thumb. They get stale as the underlying stack beneath them changes.
- blymphony 6y agoIsn't it a good habit to store the length of the array regardless of browser implementation? Technically, accessing a variable is simply faster than a property access on an object, and this wouldn't be a case of premature optimization either--just sound coding practice.
- adrianN 6y agoAny compiler worth its salt should do that optimization for you.
- nostrademons 6y agoThey're close enough not to matter on most modern browsers - I suspect V8 actually hoists the field access out of the loop when it compiles it (loop-invariant code motion is a really well-understood compiler optimization at this point). I would definitely put this in the premature optimization bucket. It doesn't really matter nowadays anyway, because now I write my for-each loops like: for (let elem : arr) { ... } or arr.foreach(elem => { ... }); (Well, technically now I write Android & C++ code and do leadership/communication stuff, but I brushed up on my ES6 before getting the most recent job.)
- otabdeveloper4 6y ago> I really hope we don’t end up with JavaScript code being written to be optimized well on one particular implementation of a JavaScript engine… You're late by many years, most frontend developers have been explicitly optimizing for Chrome for a long time now.
- Twirrim 6y agoFrom the linked bug: > I think I can reproduce this behavior on Figma's code base too. I've never noticed because we use Chrome pretty much exclusively for development. We're already there. Chrome dominates so much of the dev market, so naturally v8 is what people are optimising towards.
- thirtyseven 6y agoDon't worry, everyone still has to test for the captive audience of iOS Webkit users.
- kjksf 6y agoIn this particular case, they were not optimizing for Chrome. They generated the code that was obvious to generate. Because of a perf bug in WebKit they added a flag to generate less correct code that is fast in WebKit. If anything, it's a case of optimizing towards WebKit. But really, it's a case of performance bug in WebKit and them adding a work-around.
- quickthrower2 6y agoBut const/let are not equivalent, semantically to var.
- saagarjha 6y agoThey aren’t exactly the same thing, but in this case there’s a replacement being done to get essentially the same thing as a “fix” so they’re evidently close enough for this case.
- pjmlp 6y agoThis already happens regularly with C and C++, where a large majority of developers attribute to those languages compiler specific behaviours that aren't written anywhere on the standard.
- saagarjha 6y agoWell yes, there's the huge "GNU C(++)" dialect of course; if nothing else I think Clang's existence has helped there although of course it tended to adopt a lot of the things outright in the past. This is really something you always get when there are multiple non-reference implementations of something and one has significantly larger market share. But, I think a more relevant example might be people who write code prefixed with comments like "Destructure this manually. At -O2 on GCC this allows the first field to be placed in a register" which is putting waaay too much dependence on the internals of a specific implementation, beyond even whatever extensions a particular compiler claims to support.
- pjmlp 6y agoMy comment applies also to optimizations. Because I have seen those kind of discussions too many often, even back on Usenet days, like the example you point out.
- ksec 6y ago>Looks like the JavaScriptCore people are on it :) Well we know pizlonator hangs around on HN from time to time :) Especially on topic of JS and Webkit.
- saagarjha 6y agoYes, he does, as well as a number of WebKit people. The team is fairly plugged in with what's happening online, you'll also see them around on Twitter and such, which is pretty nice. Actually, there's even a large display in the office that's set to display tweets mentioning WebKit in real-time, although that might be a little less relevant for the JavaScriptCore people since they seemed to mostly WFH even pre-pandemic ;)
- dheera 6y agoAlso, I've generally decided that using "var" is a bad idea because it is way too easy to get tripped up with its funky scoping rules. I pretty much only use "let" and "const".
- emodendroket 6y agoIf it's really a concern, what are all those transpilers for if not this kind of thing?
- seba_dos1 6y ago> I really hope we don’t end up with JavaScript code being written to be optimized well on one particular implementation of a JavaScript engine… I'm afraid that ship has already sailed.