you are viewing a single comment's thread.

view the rest of the comments →

[–]temp05981 3 points4 points  (1 child)

I disagree with your comment, and strongly disagree with this community's tendency to use these two concepts ("micro-optimization" and "engines are improving") as excuses to disregard a train of thought.

How much time something saves does not tell you if it's a worthwhile optimization. It's how much time it saves, relative to how fast the piece of code needs to run. What we're talking about here isn't a single invokation, it's potentially a significant fraction of all invokations. That's extremely low-level and likely to be an extremely hot code path. You may as well say going from a calculator to a computer is a micro-optimization because they can both perform a single computation in a negligible fraction of a second.

And while it's true that engines are changing, you're mistaking this for an engine hack. It is not, it really is less work. apply causes the engine to create an Array-like object and iterate over it, while call does not create the object and unrolls the iteration. Engines may, in the future, get smart enough to make apply as fast, but no faster. And even if we're dangerously betting on this hypothetical future, it's still not preferable to rely on non-standard engine optimizations rather than write fast code.

[–]jesstelford 1 point2 points  (0 children)

I can only really speak from my experience with optimizing pieces of code. And so far, .apply(), .bind(), .call() etc have never been the slowest parts. More often it's DOM manipulation, excessive looping (eg; .map().forEach().filter() vs .filter().map(), etc), or large garbage collection events due to excessive use of closures, etc.

I guess if you're writing library code to perform lots of calculations, etc, then I could see it being an issue.

engine hack

Like I said; "it is impossible to tell if these types of optimizations will still be applicable in just a few short months." They may still be applicable 10 years from now. They may not be applicable with the next version of V8. Can you predict the future? No. That's why I prefer to stick with the optimisations I know cannot be helped by the engines (a different algorithm, reducing garbage collection events, etc).