See You Might Not Need jQuery for more.
If it's just size and speed that matters, a library worth noting is Zepto.js. It has a jQuery compatible API and it's much more lightweight. Compressed it's 9.2k vs jQuery's 81k.
And then you need just that one more thing and the NIH wheel keeps expanding. If jQuery's footprint is too big for you, there are plenty of smaller modern alternatives. Some even have a compatible API.
My 'solution' to this problem is that I use jQuery.
(and, if you're one of those people who go "Boo! Hiss!! I would never use a HUUUGE library that's 30 kilobytes", just use Zepto)
> Is jQuery's popularity fading?
Yes, with the popularity of older IE going away and modern helpers existing right in the core then people are needing jquery less and less
> What are its preferred alternatives
Plain old javascript http://youmightnotneedjquery.com/, but you can also use http://zeptojs.com/ if you like something minimal
One big thing I miss are the promises which IE does not support, but they can be polyfilled in various ways or a ton of folks are using Babel these days which has a nifty polyfill right there.
I'm working on a project right now that doesn't allow any libs/plugins, and half my code is just me hand-rolling aliases that look a lot like jQuery, because it's still more convenient that the cumbersome native DOM methods.
Instead of advocating for vanilla JS implementations, it would be more helpful if these articles pointed towards slimmer (but near-syntax-indentical) alternatives:
No.
Think about the "export to" operation. What it really means is, "write this logic with native APIs". For CoffeeScript, "this logic" is something like "create a function", so it can very easily be "exported" to something like function () {}
. For jQuery, "this logic" is "search through the document for nodes that look like this, and modify them in these ways, and X and Y and...". That's a lot of stuff. How do you implement a lot of stuff? Put it in a function. What's jQuery? A bunch of functions. jQuery is already "exported" to JavaScript.
The reason you hear people saying scripts that don't use jQuery are lighter is that dropping jQuery forces you to really think about what you're doing. With jQuery, it's easy to accidentally write a really complex instruction, one that takes hundreds of lines of code to fully express. If you don't have jQuery, you're more likely to figure out a simple way to solve your problem, and you don't need that really complex logic any more.
All that said, there are libraries like Zepto.js that aim to be very similar to jQuery at a much lighter weight.
You would place it inside a <script>
tag anywhere on the page (but typically at the bottom of the <body>
). If you click the settings button in the JS panel (on CodePen), you'll see that the page is using Zepto.js, so you'll need to load that too.
If you don't need all of the features/compatibility of jquery but still want most of the core pieces you can try out alternative frameworks designed just around the core parts. As an example take a look at http://zeptojs.com/
In regards to UA sniffing, as someone who builds lots of sites that have to be compatible with lots of various devices it's just something you need to deal with for now. As things improve and older phones go away we can eventually do away with them. Try to avoid them, but don't spend an unnecessary time working around it when there is a simple solution in front of you.
Or use a more lightweight drop-in alternative to jQuery such as zepto. Better yet, head over to microjs.com and pick a micro library that does exactly what you need and not a KB more.
There's also ender.js that helps you find and bundle together those micro libs.
EDIT: fixed link
There's nothing wrong with using jQuery to save you some typing if that's what you want to do, but a few things to consider:
You're forcing your users to download extra KBs. I mean, it's not a lot of data, no big deal, but on mobile every KB counts...
Using jQuery sometimes obscures how Javascript actually works, so even though the "real" approach might be longer, you have more control and understanding of your code.
There are "alternative" jQuery libs like Zepto that are much smaller and accomplish many of the same things. Why bring a huge toolkit to a job that only needs a wrench and a hammer?
Again, nothing wrong with using jQuery if you feel it's worth it, just some stuff to think about.
In particular, I'm reminded of the jQuery promises fiasco, where jQuery couldn't implement the Promises spec properly for a long time, causing many devs to not understand how promises are used and causing a lot of damage to Promises in the public eye. A number of people had to fight to correct these misunderstandings, and it slowed down Promises adoption because of the popularity of jQuery.
http://zeptojs.com the website provides way better information.
>Zepto is a minimalist JavaScript library for modern browsers with a largely jQuery-compatible API. If you use jQuery, you already know how to use Zepto.
Technically the main reason for using jQuery was patching over the most egregious cross-browser bugs and incompatibilities.
The second reason (not far behind) was getting a set of APIs which didn't want you to stab your eyes out with rusty forks. And as you note the nice fluent DOM APIs (including all the events delegation stuff) are still nowhere near the actual standard DOM, though I guess you can get it via a lightweight implementation of the API which just assumes implementations are correct e.g. Zepto.
The third one was various shortcuts for animations, selection and the like, and some object-related API (e.g. the Array-based utility functions)
Facebook did a cache study -> FB Cache study - and they probably push more code updates than the "normal" site but they found that
>The initial results were surprising to us: 25.5% of all logged requests were missing the cache. We split the data by interface, desktop and mobile, and still saw the same breakdown: 24.8% of desktop requests and 26.9% of mobile were missing the cached image. Not what we expected, so we dug in more.
and
> We can also look at what percentage of users are getting an empty cache. On average, 44.6% of users are getting an empty cache. That's right about where Yahoo was in 2007 with its per-user hit rates
its fairly interesting to read if you're into this stuff haha.
as for a jquery "lite" there are libraries that like that - as for usage your mileage may vary of course - for instance:
and microjs.com might have a library you can use.
If you need a "smaller" jQuery I can recomend Zepto.js or minified.js. Both are similar to jQuery but much more light weight.
If you are doing this out of curiosity or fun document.querySelector
, HTMLElement.prototype
, Object.assign
and Array.from
might be helpful.
Happy coding!
> Even JQuery there isn't much reason to include nowadays if you're going to write plain JS/TS. I've never had a serious issue with DOM manipulation in modern JS.
Some who feel differently might consider the lightweight alternative, Zepto.js. It is largely compatible with jQuery.
My first guess is Zepto, a lightweight library that does some of what jQuery does and is compatible with jQuery's API: http://zeptojs.com/
I also found an early Ajax library at crossbrowserajax dot com that was designed to work even in IE5 (the oldest browser that supports asynchronous server communication), even with disabled ActiveX.
Then I remembered MicroJS, and it reminded me of Reqwest: http://microjs.com/#ajax
there is another js library called 'zepto' does this, its api is 100% jQuery-compatible. http://zeptojs.com
my objective was a bit different: covering the same functionalities, keeping it lightweight and performing better (especially on mobile devices) at the same time.
I think jQuery API brings some complexity with itself, it basically hits the client performance. Benchmarks are available on https://larukedi.github.io/laroux.js/benchmarks.html