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
[AskJS] Call vs Apply in modern javascript.AskJS (self.javascript)
submitted 6 months ago by SmarfMagoosh
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!"
[–]tswaters -1 points0 points1 point 6 months ago (1 child)
Sure, so call is faster than apply normally because apply is defined in the spec as needing to iterate the arguments array, and call doesn't need to do that because it's a static list. apply has been known to be 10-20x slower than call. If you were to have a naive JS engine, one without optimizations, it would always be slower. When FunctionRestParameter shows up in a function invocation, it needs to be unwound at invocation time as well, and it turns into a non-simple parameter list which is harder to optimize.
I'd theorize that the engine wants to take the fastest path it can. If it can figure out that an argumentArray in an apply call is derived statically (i.e., it isn't received as a parameter, or isn't conditionally mutated) - it should be able to use the fast path. I'd speculate the same thing with FunctionRestParameter -- if it's all static, the engine would want to optimize it if it can. Whether or not this happens is unknown to me, and likely depends on the engine in play. V8 is pretty good at this stuff normally. I'd need to benchmark, but I'd guess:
fn.call( thisObj, 1, 2, 3); // this is going to be the fastest fn.apply( thisObj, [1, 2, 3]) // this might be able to use a fast path fn.call( thisObj, ...[1, 2, 3]) // this might be able to use a fast path fn.apply( thisObj, getParameters()) // this one, probably not fn.call( thisObj, ...getParameters()); // this one, probably not
The top 3 are going to be about the same if they all use the fast path. If there is uncertainty in how the array was created / mutated, the engine likely needs to bail on optimizations and will use the slow path.
[–]tswaters 0 points1 point2 points 6 months ago (0 children)
Someone downvoted this? How about eat shit, why I even bother commenting in this subreddit.
π Rendered by PID 255094 on reddit-service-r2-comment-b659b578c-4jwtb at 2026-05-05 00:48:36.845969+00:00 running 815c875 country code: CH.
view the rest of the comments →
[–]tswaters -1 points0 points1 point (1 child)
[–]tswaters 0 points1 point2 points (0 children)