4 ms·
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 suppo
by rchaud 5y ago
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/ http://youmightnotneedjquery.com/
- ThePadawan 5y agoI 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.
- rchaud 5y agoIf 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.
- FinalBriefing 5y ago> 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.
- andybak 5y agoGosh. 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.
- dageshi 5y agoWhen 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...
- edflsafoiewq 5y agoIncidentally, there's a few lightweight jQuery-likes you can use if you just want a nicer syntax. Zepto and umbrella are popular.
- rchaud 5y agoThe modern web is a soup of React, Vue and Angular packages with numerous dependencies. Couldn't they just use jQuery UI?
- Zababa 5y ago> 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.
- gred 5y agoI 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.
- rchaud 5y agoIn 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.
- oatmealsnap 5y agoThat'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.
- motogpjimbo 5y agoYou'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.
- chrismorgan 5y agoThat 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.
- johannes1234321 5y ago> 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());
- 5y ago
- FinalBriefing 5y agoI 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.
- Legion 5y ago> 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."
- rchaud 5y agoWe 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)
- mvolfik 5y agonote 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_Partitioning#network_partitioning https://developer.mozilla.org/en-US/docs/Web/Privacy/State_P...
- iso1631 5y agoI 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
- goldenkey 5y agoYou could have used async/await and fetch along with a polyfill if you are concerned about compatibility with older browsers. ;-)
- Zababa 5y agoMaybe 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.