4 ms·
The quality of this article is readily apparent in the first recommendation. Global variables are slow... because of having to walk scope chains? Uh, no. Scope
by jcranmer 9y ago
The quality of this article is readily apparent in the first recommendation.
Global variables are slow... because of having to walk scope chains? Uh, no. Scopes are static properties (well, once the with statement was eliminated--now you know why that doesn't exist anymore): the compiler only has to look up the scope of the variable during compilation, which means it carries no penalty by the time you get to a JIT. The reason why global variable lookups can be slow is that global variables are often properties on objects with non-trivial access costs (such as a DOM window, if you running on a webpage), as opposed to an inaccessible lexical environment where all references are statically known.
(It's also somewhat disingenuous that all of the coding tips are about keeping v8 happy, as if v8 were the only JS JIT that mattered or v8 characteristics were somehow universal in JS JITs, neither of which is the case).
Running through the some of other bits:
* I'm not sure homogeneous arrays is an optimization benefit for other JITs than v8.
* Actually, traditionally JIT compilers focus on hot loop nests as well as functions. The original TraceMonkey JIT (first JS JIT) considered loops a starting traces, for example.
* Changing shape of objects does matter for monomorphic calls. Although not all JITs are as insistent on monomorphism as v8.
* True, but isn't this obvious?
* Last I checked (admittedly quite a while ago), JITs tended to optimize based on shape, not class. So there's no difference in performance between the two cases.
* Or you could use for/of. Or you could just never use sparse arrays because no JIT likes having these holes. I thought you were giving performance tips, it seems weird not to mention one of the more universal ones here.
* Try/catch is generally free if you don't throw an exception.
I could go on, but it's not worth it at this point...
- thealfreds 9y agoThe try/catch wasn't always that case. I'm having trouble finding the blog from one of the v8 developers who went into details about it and other optimizations. It was on HN a while back. Here is a perf test with various v8 versions (as you noted not only JIT but going going with the authors favorite). https://github.com/davidmarkclements/v8-perf https://github.com/davidmarkclements/v8-perf
- jcranmer 9y agoMy recollection is that the nonperformance of try/catch is something specific to the v8 JIT. Historically, the zero-cost exception model of try/catch has you do absolutely nothing with the data until someone throws an exception, at which point you work out how to map the return address to the appropriate catch handler.
- tkone 9y agov8 used to not be able to optimize blocks containing try/catch, which is why if you needed it, you relegated it to a function containing just the try/catch block. but since async/await relies on the use of try/catch, last november's (october?) v8 release can optimize try/catch. plus, it was only ever a v8 issue, not an issue in javascriptcore nor tracemonkey.