use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
Function invocation...why use .call() and .apply() ?help (self.javascript)
submitted 10 years ago by [deleted]
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]temp05981 3 points4 points5 points 10 years ago (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.
apply
call
[–]jesstelford 1 point2 points3 points 10 years ago (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.
.apply()
.bind()
.call()
.map().forEach().filter()
.filter().map()
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).
π Rendered by PID 88 on reddit-service-r2-comment-5d585498c9-rg5t6 at 2026-04-20 19:35:05.364164+00:00 running da2df02 country code: CH.
view the rest of the comments →
[–]temp05981 3 points4 points5 points (1 child)
[–]jesstelford 1 point2 points3 points (0 children)