News for hackers. Stuff that matters.
Re:I loved jQuery, and still do (2019) by rchaud on Tuesday June 29, 2021 @11:58AM (#27677744)
Every time anything about jQuery is posted, someone will inevitably post the "You might not need jQuery" site. [0]

I look at that site and laugh. Is this supposed to be making the case ~against~ jQuery? Maybe the audience for that site are seasoned developers for whom the vanilla JS syntax is easily understood. Everybody else is going to look at those code snippets and think jQ is the way to go.

For people that have struggled through HTML and CSS, there is perhaps nothing more deflating than to realize that even basic UI interactivity requires JS. But with jQuery the code snippets needed are short and understandable. Vanilla JS in comparison looks like an entirely different language.

[0] http://youmightnotneedjquery.com/

Re:I loved jQuery, and still do (2019) by Legion on Tuesday June 29, 2021 @03:10PM (#27680367)
> Is this supposed to be making the case ~against~ jQuery?

It's supposed to be making a case against jQuery if you're pulling in the whole library just to do this one thing.

I've always read the site as "here's how to do it in native JS, yes it's uglier, but if you only need this, you can skip pulling in another dependency."

Re:I loved jQuery, and still do (2019) by rchaud on Tuesday June 29, 2021 @04:20PM (#27681209)
We use React, Angular and Vue for websites where 99% of the functionality could be done without monstrosities like CSS-in-JS.

Over-engineering websites has been around forever. That's partially why jQuery is modular, letting you pick what you need.This covers the 'use jquery for just this one thing' crowd. And if they're lazy, well, they can hang their heads in shame because they have added an entire 83 KB to the page's initial load time. /s

(unless the browser has a CDN cache stored, in which case it wouldn't impact load time at all)

Re:I loved jQuery, and still do (2019) by mvolfik on Wednesday June 30, 2021 @08:11AM (#27687516)
note that browsers' cache has recently been changed so that it's isolated for each origin - if you visit a.com which loads jQuery, it doesn't speed up loading jQuery on b.com at all https://developer.mozilla.org/en-US/docs/Web/Privacy/State_P...
Re:I loved jQuery, and still do (2019) by gred on Tuesday June 29, 2021 @12:23PM (#27678118)
I can see where you're coming from, but just wanted to add a counterpoint.

I recently removed jQuery from a website, and the YMNNJQ website was one of the bits of info that gave me the nudge to do that. The website in question only used jQuery in a handful of places, it was one of the few third party libraries being used (so the % size savings was significant), and I'm fairly fluent with JS... The website simply hadn't gone through a major update in the past 5 or 6 years, and the browser landscape has shifted enough in that time that the pros/cons weigh differently now.

So I think there are scenarios where this info is sometimes useful in the "I might remove jQuery" sense, and not in the "wow, I'm definitely keeping jQuery!" sense.

Re:I loved jQuery, and still do (2019) by rchaud on Tuesday June 29, 2021 @01:09PM (#27678745)
In your example, switching from jQuery was just a matter of personal preference. There's nothing wrong with that.

My original comment left out some context: on HN, a lot of the hostility to jQuery is either cultural or gatekeeping oriented. Cultural opposition is of the 'not invented here' variety, e.g. 'Why use Dropbox when you could do this w/ rsync? Gatekeeping-related opposition is where it's implied that you shouldn't call yourself a dev if you use jQuery and not vanilla JS.

That is the kind of discussion that happens nearly every time a link to YMNNJQ is dropped.

Re:I loved jQuery, and still do (2019) by oatmealsnap on Tuesday June 29, 2021 @02:17PM (#27679670)
That's not how I see jQuery hate.

jQuery promotes bad development patterns, AND brings a cost for end users. Developers will do themselves and futures devs a favor by not choosing to use it. Is it really gatekeeping to expect people to be aware of commonly used 10 year old features?

I assume developers who share YMNNJQ are just sick of inheriting messy projects built with jQuery. I know I am.

Re:I loved jQuery, and still do (2019) by motogpjimbo on Tuesday June 29, 2021 @04:08PM (#27681059)
You've never inherited a terrible, unmaintainable React or Angular app? I have on multiple occasions, and in fact my team is dealing with one right now. I'd prefer a jQuery mess to an Angular/React mess any day of the week. With old-school jQuery apps, at least I can get them to build and can debug the control flow.
Re:I loved jQuery, and still do (2019) by heavyset_go on Tuesday June 29, 2021 @07:56PM (#27683330)
A couple of years ago I inherited a client's static site that was written in React before Next.js took off. It is a real accomplishment to make a static site as unnecessarily unmaintainable and convoluted as that one was.

Part of my screening process is making sure something like that doesn't fall in my lap again. Even static sites aren't safe these days.

Re:I loved jQuery, and still do (2019) by rchaud on Tuesday June 29, 2021 @04:30PM (#27681320)
> jQuery promotes bad development patterns

If you do web development as a job, it's your responsibility to stay up to date on what best practices are. With EC2015 being several years ago now, jQuery wouldn't be a learner's starting point today.

Those that developed bad habits during jQuery's heyday a decade ago may still be writing bad code. And it's someone else's job to clean that up. That's the circle of life, and it's hardly exclusive to jQuery users.

Re:I loved jQuery, and still do (2019) by thera2 on Tuesday June 29, 2021 @04:10PM (#27681088)
The article explains why jQuery doesn't promote bad development patterns. That is a consequence of bad development and a misunderstanding of what jQuery is.
Re:I loved jQuery, and still do (2019) by shkkmo on Tuesday June 29, 2021 @02:27PM (#27679798)
> it's implied that you shouldn't call yourself a dev if you use jQuery and not vanilla JS.

I've never seen that. The closest I've come to seeing that is that comments that imply that using jQuery indicates lower levels of experience as a developer.

> Cultural opposition is of the 'not invented here' variety, e.g. 'Why use Dropbox when you could do this w/ rsync?

How is that "not invented here"? That seems more like a preference for open source software.

I looked at submission history and I don't really see either of those attitudes prevalent in a quick scan of the comments:

https://news.ycombinator.com/from?site=youmightnotneedjquery...

Re:I loved jQuery, and still do (2019) by chrismorgan on Tuesday June 29, 2021 @12:39PM (#27678337)
That site is generally aiming at providing exact replacements, and quite a few of its examples are not great for various reasons:

Some use a bad technique; e.g. “Empty” removes the first child repeatedly, which is O(n²) in many environments, rather than removing the last child repeatedly, which is O(n), or `el.innerHTML = ""` which is shorter and more efficient in general)

Some suffer because they’re supporting IE which no one should do any more (fite me); e.g. “JSON” is long because it’s using XMLHttpRequest, but the Fetch API would make it vastly shorter (`data = await (await fetch('/my/url')).json()`—as an aside, suffix await like Rust settled on is so much nicer: `data = fetch('/my/url').await.json().await`); and “Each” should use `document.querySelectorAll(selector).forEach((el, i) => …)`; and “Matches Selector” is just `el.matches('.my-class')` now.

Also some look bad because jQuery optimised an operation that isn’t terribly common or should generally not be done, such as most of the width/height things (most uses of measuring element dimensions in JS are unnecessary, bad or wrong; aim to use CSS most of the time), “Index” (needing to know that an element is the nth child element is decidedly esoteric), “Type” (if you use this, you’re almost certainly doing the wrong thing), and “Parse HTML” (picture me running away, screaming in horror; this method is practically a primed security grenade). And remember, jQuery having optimised these things means you’re paying to ship that stuff that you’re not using.

And finally some seem more complex because they’re matching some unnecessary jQuery complexity, e.g. in “Set Height” (and alike in “Set Width”), $(el).height(…) takes a string, a number, or a function that returns a string or a number—but you’d be wiser to write `el.style.height = string;` or `el.style.height = number + 'px';` rather than being unnecessarily flexible.

Most people that use jQuery use a tiny subset of its functionality. The site is about showing that most uses of jQuery could actually dispense with it at little cost, if they’re the sort that value performance and such and want to ship less unnecessary code.

Re:I loved jQuery, and still do (2019) by johannes1234321 on Tuesday June 29, 2021 @12:58PM (#27678596)
> Fetch API would make it vastly shorter (`data = await (await fetch('/my/url')).json()`—as an aside, suffix await like Rust settled on is so much nicer: `data = fetch('/my/url').await.json().await`);

const data = await fetch('/my/url').then(r => r.json());

Re:I loved jQuery, and still do (2019) by chrismorgan on Tuesday June 29, 2021 @02:37PM (#27679942)
Hmm, forgot about that style. Where you only have prefix await, .then() can end up nicer (though I might split it into `const response = await fetch('/my/url'); const data = await response.json();` as yet another style), but if you can have suffix await, I reckon it’s much nicer. Goes well with fluent API styles.
Re:I loved jQuery, and still do (2019) by johannes1234321 on Wednesday June 30, 2021 @05:15AM (#27686519)
> Hmm, forgot about that style. Where you only have prefix await, .then() can end up nicer

Mind that in this example the then is a little nicer only by chance that the continuation is quite trivial. If you'd need something more complex than the single function call it is annoying and if you do any later change to this code, you likely have to change this as well.

I typically go to the multiple lines, but wanted to show that there is a kind-off suffix await.

I am not used to suffix await, as i like to spot the places where i jump out of the coroutine easily. But maybe that would change if I'd use languages with suffix await more :)

Re:I loved jQuery, and still do (2019) by romellem on Tuesday June 29, 2021 @02:17PM (#27679663)
Why is removing the first element repeatedly O(N^2) while removing the last one O(N)? Is it because its cheaper for the browser to remove things from the end of a NodeList rather than the front? (similar to how .pop() is cheaper than .shift() for larger arrays?)
Re:I loved jQuery, and still do (2019) by chrismorgan on Tuesday June 29, 2021 @02:35PM (#27679904)
On reflection, “many” wasn’t the best word to use; “some” would be better.

In the past, I believe that most or all browser implementations used array-like structures to do various things like this, and so removing the first element entailed shifting all the remaining elements, whereas popping from the end doesn’t require shifting any elements.

More recently, I believe that most or all browser implementations have shifted to linked-list-like structures, with bad patterns like this being a cited reason. (I don’t know the full story by any means, but I’m inclined to be disappointed by the switch, because for most access patterns arrays aren’t a problem at all, and use less memory, sometimes even being a smidgeon faster. But although I suspect they’d have better typical performance and certainly better memory size, they have vastly worse worst-case performance, being approximately O(n) instead of O(1) for some operations.)

Remember also when talking of NodeList like this that it’s a type for JavaScript to consume; what browsers use internally for representing their DOM is an implementation detail. A leaky one, certainly, in performance details like this and types like HTMLCollection and NodeList (especially live NodeLists), but an implementation detail nonetheless.

Re:I loved jQuery, and still do (2019) by ThePadawan on Tuesday June 29, 2021 @12:13PM (#27677969)
I agree that the vanilla JS snippets seem hilariously more complicated, but simplicity isn't everything.

In a corporate environment, if "I need to include jQuery as a dependency" involves asking another team how to set up custom deployment or whatever, and that is the difference of days between deploying a feature or not, then I'll definitely take the hit of 10 more SLoC vanilla JS.

Re:I loved jQuery, and still do (2019) by rchaud on Tuesday June 29, 2021 @01:39PM (#27679221)
If it's a corporate environment, the standards are different, understandably. But the hostility to jQuery is also primarily concentrated among corporate devs that seemingly cannot imagine that jQuery might be valuable for use cases other than their own.

Not every site out there is an database-driven SPA that needs QA review and a deployment pipeline each time an update is made. Lots of sites are just simple WordPress or even raw HTML pages that need a little bit of a facelift, with animations or transition. jQuery does the trick, and as a bonus, it's a much gateway drug to JS than the official ES6 documentation could ever be.

Re:I loved jQuery, and still do (2019) by FinalBriefing on Tuesday June 29, 2021 @02:39PM (#27679967)
> little bit of a facelift

Proceeds to download 30kb (compressed) swiss army knife of a library when just a toothpick would do.

jQuery is about as much of a gateway to ES6 as pot is to heroin. Devs who still use jQuery have had 10+ years to learn `document/element.querySelectorAll()`. Longer for event listeners.

Re:I loved jQuery, and still do (2019) by dageshi on Tuesday June 29, 2021 @03:58PM (#27680956)
When you learnt something with better more easily readable syntax, it's kinda hard to make yourself use something worse to save 30kb, when that saving will be lost with a single image on the page you're loading...
Re:I loved jQuery, and still do (2019) by edflsafoiewq on Wednesday June 30, 2021 @03:45AM (#27686026)
Incidentally, there's a few lightweight jQuery-likes you can use if you just want a nicer syntax. Zepto and umbrella are popular.
Re:I loved jQuery, and still do (2019) by andybak on Tuesday June 29, 2021 @03:39PM (#27680762)
Gosh. 30kb.

> Devs who still use jQuery have had 10+ years to learn

Or possibly we learned it in 9 years 11 months ago and still think jQuery was a more elegant solution.

Re:I loved jQuery, and still do (2019) by rchaud on Tuesday June 29, 2021 @04:35PM (#27681385)
The modern web is a soup of React, Vue and Angular packages with numerous dependencies. Couldn't they just use jQuery UI?
Re:I loved jQuery, and still do (2019) by Zababa on Tuesday June 29, 2021 @06:52PM (#27682795)
> Proceeds to download 30kb (compressed) swiss army knife of a library when just a toothpick would do.

I agree that 30kb can be big, especially since it's JS that needs to be executed, but on the other hand if the toothpick means you save a minute, you can use it to optimize an image and end up with a lighter and faster website.

Re:I loved jQuery, and still do (2019) by iso1631 on Tuesday June 29, 2021 @03:13PM (#27680400)
I assume that's a pro-jquery site

I write random bits of scripts for internal use, I'm happy enough to drop the min.js in with my index.js, index.css and index.php files, especially if it means I can continue to use

  getJSON('/my/url', function(data) {
  
  });
Rather than

  var request = new XMLHttpRequest();
  request.open('GET', '/my/url', true);
  
  request.onload = function() {
  if (this.status >= 200 && this.status < 400) {
    // Success!
    var data = JSON.parse(this.response);
  } else {
    // We reached our target server, but it returned an error
  
  }
  };

  request.onerror = function() {
  // There was a connection error of some sort
  };

  request.send();
The 90KB of jquery is nothing compared with the amount of crap modern javascript frameworks put in on other sites.

I've just looked at one page I wrote which has a little jquery in it. The outputted data is 673kb, which takes nearly half a second to generate and load. The jquery file being static is down in 0.01s.

This isn't a problem

Re:I loved jQuery, and still do (2019) by goldenkey on Tuesday June 29, 2021 @03:32PM (#27680665)
You could have used async/await and fetch along with a polyfill if you are concerned about compatibility with older browsers. ;-)
Re:I loved jQuery, and still do (2019) by Zababa on Tuesday June 29, 2021 @06:48PM (#27682762)
Maybe that's just me but I find the async/await stuff way harder to understand than a simple callback. Sure when you're going through callback hell you're glad to have promises and the syntaxic sugar, but for simple things the callback makes sense, especially since it's already how you use map and filter in JS.
Re:I loved jQuery, and still do (2019) by FinalBriefing on Tuesday June 29, 2021 @02:27PM (#27679810)
I think that websites needs an update, too. It would be a bit more convincing if they didn't try to support IE, and explained why the native methods are better.

Starting with the Ajax exmaple isn't the best way to get people on board, either. Until the Fetch API was widely adopted, I also reached for a library if I had more than one very simple API call.