Cash: An absurdly small jQuery alternative for modern browsers
github.com
github.com
cash.min.js is 7.8kb once downloaded, for anyone coming to this thread after me.
Nice work all around, is this comparable with Zepto? What features are missing from Cash that you guys hope to add?
https://github.com/kenwheeler/cash/blob/3cb498ca7c5776b8cadc...
curl https://rawgit.com/kenwheeler/cash/3cb498ca7c5776b8cadcce588... | wc -c # 9282
curl https://rawgit.com/kenwheeler/cash/3cb498ca7c5776b8cadcce588... | gzip | wc -c # 3225
curl https://cdn.jsdelivr.net/cash/1.0.0/cash.min.js | gzip | wc -c # 2636
Jquery functions Zepto has that Cash does not: ajax, contents, hide/show, map, offset, offsetParent, replaceWith, scrollLeft, scrollTop, size, slice, submit, toggle, unwrap, wrap, wrapAll, wrapInner
Zepto also has some optional plugins that aren't in the default build.
https://github.com/finom/balalaika/
If that's too much, there's an even more extreme version (379 chars):
> document.querySelectorAll('.my-selector')
Finding two unrelated projects with the same name was a kik in the pants.
Since browser compatibility is one of the primary purposes of jquery, I think it would be helpful if this library's readme said what its browser compatibility is.
var _$ = function(selector){
var els = document.querySelectorAll(selector);
return Array.prototype.slice.call(els);
}
For example: _$('span').forEach(function(el){ el.style.color = 'red' });74 kilobytes might have been a lot a few years ago, these days it is (unfortunately) mostly "noise". Particularly if it is not a real alternative but only a subset of the original functionality.
I didnt say you should add an invisible 1MB image to your site but that a few kilobytes do not make such a difference anymore (particularly when you lose functionality at the same time).
All these jQuery alternatives are nice and such but are not actually bringing much to the table. Yes with each of them you save a few kilobytes but also lose a lot of features. In that case you can go native straight away (querySelectorAll) and save any download in the first place.
And, mobile connections are not that poor anymore as well. Certainly also depends on the region in the world but overall they are getting closer to fixed one every year.
But yes, eventually it depends on the use case and I am sure there are cases where a trimmed down library might be useful. They wont be the common ones though
Just a random example: my band's rehearsal space is under ground and has very poor connectivity. Sometimes I still want to look something up down there. In that situation, I really appreciate websites that download minimal amounts of code.
And I think lots of people have equivalent situations, whether it's their country house, their month in rural Thailand, their sailboat, prison, the wifi on the night bus between Cluj-Napoca and Budapest, etc etc etc.
Since I've been annoyed by heavy websites so many times, often in precious or precarious situations, I have a principle of caring about every kilobyte (and caring about offline functionality, too).
As for whether using this library just means you need to import more libraries to cover for jQuery's other features, that's an empirical question that also depends on use case.
My metrics are aproximately 1:5:7 for lines of code:tests:documentation and I might do the same check later on for jquery, but from reading their source and documentation I think they might be in a similar situation.
Including jQuery in browsers is kind of a ridiculous idea...
But if you're going to argue over 5KB perhaps you shouldn't be using JavaScript or CSS. If bandwidth matters that much, plaintext should keep the punters happiest.
I never had issues with older browsers because most of those issues are css related. And imho if you need Javascript to fix your layout you are having other issues.
I still go for jquery for the likes of http requests and css selectors. Which libraries do you use for those tasks now?
(requests) https://developer.mozilla.org/en/docs/Web/API/Fetch_API http://caniuse.com/#feat=fetch
(css selectors) https://developer.mozilla.org/en-US/docs/Web/API/Document/qu... https://developer.mozilla.org/en-US/docs/Web/API/Document/qu... http://caniuse.com/#search=queryselector
Self-host this file, people.
At 3.8k gzipped, I'd just inline the minified version...
If you haven't tried writing "raw" JS in a while give it a try. You might be surprised.
$(element).is(selector)
to the standard: element.mathes(selector);[1] https://github.com/umbrellajs/umbrella/blob/master/src/plugi...
It's not ready.
fetch(url)
.then((res) => {
if (res.statusCode < 200 || res.statusCode > 299) {
throw new Error("Why do I have to write this with every request?");
}
});If it's really that important to you, you can always create your own wrapper around fetch
function fetch2(uri) {
return fetch(uri).then( (res) => {
if (res.ok) {
return Promise.resolve(res);
} else {
return Promise.reject("Request failed with status "+res.status+" ("+res.statusText+")");
}
} );
}
fetch2('some-url')
.then( (res) => {
console.log('Success', res.status, res.statusText);
})
.catch( (err) => {
console.log(err);
});
[1]: https://developer.mozilla.org/en-US/docs/Web/API/Response/ok- Zepto : http://zeptojs.com/
- Umbrella JS (mine): http://umbrellajs.com/
- Nano ajax: https://github.com/yanatan16/nanoajax
Maybe I'm missing something?
Facebook did a related study more recently and found that large numbers of users had cold caches (https://code.facebook.com/posts/964122680272229/web-performa...).
`Array.prototype.slice.call(document.querySelectorAll(selector)).forEach(cb)`
vs
`$(selector).each(cb)`
[0] https://developer.mozilla.org/en-US/docs/Web/API/NodeList
I found this one, but it's from 2014 so I'm not sure it's the same Cash.
Sadly, JQuery and its continued success isn't attributed to its efficiencies. It's community support, adoption, and heavy downstream reliance will keep it around as a standard for eternity.
[2] github.com/umbrellajs/umbrella
You can always check yourself [1], but of course it's not what you asked.
C3js made a good impression otherwise and flot seems to be dead (no activity on https://github.com/flot/flot, or is there a living fork?) making that decision hard.