r/programming • • Jun 09 '16

jQuery 3.0 Released!

https://blog.jquery.com/2016/06/09/jquery-3-0-final-released/
1.1k Upvotes

354 comments sorted by

View all comments

Show parent comments

12

u/puhnitor Jun 10 '16 edited Jun 10 '16
[...document.querySelectorAll('.goodbye')].forEach(goodbye => goodbye.classList.add('selected'));

80

u/EternalNY1 Jun 10 '16

forEach

Yes if you want all versions of IE dropped as well as Safari.

Or bring in jQuery, small and fast and almost guaranteed to be cached locally anyway, and get it fully compatible in 1 (shorter) line without the lambda "arrow function".

So, in your case, we'd have (works on some browsers):

[...(document.querySelectorAll('.goodbye')].forEach(goodbye => goodbye.classList.add('selected'));

Works on almost all browsers:

$(".goodbye").addClass("selected");

15

u/Elsimir Jun 10 '16

forEach is the least of the compatibility problems there too, there's a spread operator and an arrow function.

6

u/EternalNY1 Jun 10 '16

Was trying to keep it simple :)

1

u/jacobrask Jun 10 '16

jQuery, small and fast and almost guaranteed to be cached locally

That you keep claiming this in every comment does not make it any more true. There is a huge fragmentation in jQuery versions, so even if 100% of all jQuery sources were served from the same CDN the chance of them having your particular version cached is not great. See https://www.stevesouders.com/blog/2013/03/18/http-archive-jquery/

2

u/EternalNY1 Jun 10 '16

That you keep claiming this in every comment does not make it any more true.

Where's that little emoticon with the smiley smashing his head against the wall? :)

Ok, no problem.

There's only one real way to answer this question, and since I don't work in your office ....

What percentage of your bandwidth utilization from the server is currently being taken up by serving jQuery?

Almost no one can answer that, and that is the problem. I can answer it, I know down to a single byte how much the server is sending out jQuery as opposed to the thousands of other things in a given request.

People spend all day worrying about "all these bytes being sent" when they are not looking at the numbers.

If you have the numbers, then you don't even need to be posting this, because you understand what you're doing, as do I.

1

u/cybercobra Jun 10 '16

forEach works in IE9+ and in Safari according to MDN...

1

u/EternalNY1 Jun 10 '16

forEach works in IE9+ and in Safari according to MDN...

I quoted the wrong part. Copy/paste issue.

Note that I did say:

without the lambda "arrow function".

The link you are looking for is here:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions#Browser_compatibility

0

u/[deleted] Jun 10 '16

[deleted]

11

u/tech-ninja Jun 10 '16

But then your intent is not clear anymore. Anyway I wouldn't be concerned about the speed of forEach vs map.

16

u/EternalNY1 Jun 10 '16

Anyway I wouldn't be concerned about the speed of forEach vs map.

A lot of people would be!

And therein lies the problem.

Programmers working with JavaScript sometimes seem like they are running the particle colliders at CERN.

When one image on your page is bigger than jQuery, and you are worried about having to "bring in all of jQuery when we only use half of it", you need to take a deep breath and start again.

Who does have to worry about this? The Google's and Facebook's of the world, where a change that is a few bytes more expensive can cost millions of dollars.

Who doesn't? Almost everyone else.

3

u/[deleted] Jun 10 '16

exactly, almost never becomes a problem and I build some pretttttty fucking meaty clients.

6

u/enkideridu Jun 10 '16

Please don't use maps to perform mutations

2

u/Labradoodles Jun 10 '16

So I understand the logical reasoning behind not doing mutations with a functional.method with an emphasis on immutability but in this case I don't see much if a problem would you care to elaborate what's wrong with it?

4

u/puhnitor Jun 10 '16

The intent is no longer clear. .map has a very clear intent - give me a new array with some transform performed to all the elements. Don't mutate the original array.

.forEach is the opposite.

3

u/kqr Jun 10 '16

It communicates conflicting intentions. The map says "any side effects are a mistake and should be put in the bug tracker" while the loop body says "I'm nothing but a side effect." That kind of thing makes the code harder to read for the next person two years down the line.

→ More replies (7)

2

u/[deleted] Jun 10 '16

in client work, how often is forEach actually an issue? use the most succing code then take a quick look at the profile against a large set of data.

for less code, easier to read, and of course if isn't causing jank, it makes sense to go with forEach.

→ More replies (4)
→ More replies (1)
→ More replies (12)

10

u/thebuccaneersden Jun 10 '16

that... that does not look as clean... just sayin...

24

u/EternalNY1 Jun 10 '16
  • Not as clean

  • Not as compatible

All fixed in a 34kb lib that likely will not even be downloaded because the client already has it.

4

u/earthboundkid Jun 10 '16

That's flashy, but even just querySelector basically does the hard part that jQuery used to do.

2

u/vplatt Jun 10 '16

Boo! Hiss!

2

u/puhnitor Jun 10 '16

VanillaJS is the best framework.

18

u/vplatt Jun 10 '16

I understand what you're saying, but if I have to choose between:

$(".goodbye").addClass("selected");

And:

[...document.querySelectorAll('.goodbye')].forEach(goodbye => goodbye.classList.add('selected'));

It's not much of a contest.

1

u/[deleted] Jun 10 '16

Meh, that works, but something like the bind syntax which is in proposal stage looks a lot cleaner.