Live data from Hacker News

Replacing jQuery (110kb) With UmbrellaJS (8kb)

bennadel.com

11–20 of 55 posts

Re: Replacing jQuery (110kb) With UmbrellaJS (8kb)

#11
post #4

I think zepto.js is a better choice. It's slightly bigger than cash but much closer to jquery (for example it has $.ajax) https://zeptojs.com

Cash's maintainer here. I don't think this is true actually. Zepto supports some methods that Cash doesn't, but you probably shouldn't use them to begin with, like $.ajax, $.isArray, $.fn.animate etc. In 2022 either better built-in solutions exist or better specialized tiny libraries exist. Everything that is supported by both Zepto and Cash should either work identically or Cash's implementation should be closer to…

Umbrella JS maintainer here (the library this HN thread is supposed to be about). At 2.8kb gzipped UmbrellaJS doesn't support partial builds because that'd be a bit funny.

Also, and it seems to be something the author of the article likes a lot, Umbrella JS is closer to Javascript than the libraries you mention (& jQuery). Sure that means you need a bit of editing your code (which is made easy by being able to load both jQuery and Umbrella at the same time), but in the end you have a lot better result. Plugins are just methods extending the prototype, loop methods have similar arguments as Array's methods, which in turn makes it easier than ever to use arrow functions, etc.

But IMHO the killer feature of Umbrella JS is that it's quite well documented, every method has at least an example but usually few, where e.g. cash doesn't have any example interleaved.

Basically this would be my recommendation/conclusion:

- If you have thousands of lines of Javascript/jQuery, I'd recommend Cash/Zepto instead.

- If you just use jQuery for few methods/interaction, 100% go with Umbrella JS.

Re: Replacing jQuery (110kb) With UmbrellaJS (8kb)

#12
I find it funny that I shared this article because I was proud that someone talked about my little library Umbrella JS, and so far 5 out of the 5 top-level comments are people recommending their/other libraries or ways of avoiding jQuery (and not 2 agree with the best way of avoiding jQuery!).

Re: Replacing jQuery (110kb) With UmbrellaJS (8kb)

#13
post #5

I prefer `querySelector` and bare ` ` tags for quick and dirty jQuery replacement. If you haven't keeping up with all the new script-tag parameters, I suggest looking at this: https://gist.github.com/jakub-g/385ee6b41085303a53ad92c7c8af... Choosing type=module is usually better choice than classic script tags like in the article. Module tags fetches (if any modules is used) asynchronously, and executes after the pars…

That's what I did! I saw a lot of people recommending doing basically:

    const $ = sel => [...document.querySelectorAll(sel)];
But then you get to it and are adding a lot of classes, so you might add a couple of helper methods:

    const addClass = (col, cls) => col.forEach(el => el.classList.add(cls));
    const removeClass = (col, cls) => col.forEach(el => el.classList.remove(cls));

    addClass($('ul li'), 'list-item');
Oh and you might need to inject content before/after, that's a bit trickier but thanks to the classic You Might Not Need jQuery[1] we know how to do it:

    const insertBefore = (col, html) => col.forEach(el => el.insertAdjacentElement('beforebegin', html));
    const insertAfter = (col, html) => col.forEach(el => el.insertAdjacentElement('afterend', html));

    insertAfter($('ul li:last-child'), 'New todo item');
Keep going a bit like that, until you realize you are basically reinventing jQuery but pretty badly, undocumented, and untested.

Add a couple of very nice-to-haves, like chaining (instead of nesting in these examples above) and prototypes and that's what Umbrella JS is, very thin methods to manipulate the DOM and handle events. In fact, compare our "addClass" implementation in this comment to Umbrella's addClass[2], it's almost the same size but hundred times more flexible:

    // Add class(es) to the matched nodes
    u.prototype.addClass = function () {
      return this.eacharg(arguments, function (el, name) {
        el.classList.add(name);
      });
    };

[1] https://youmightnotneedjquery.com/

[2] https://github.com/franciscop/umbrella/blob/master/src/plugi...

Re: Replacing jQuery (110kb) With UmbrellaJS (8kb)

#14

Earlier quoted context omitted.

Cash's maintainer here. I don't think this is true actually. Zepto supports some methods that Cash doesn't, but you probably shouldn't use them to begin with, like $.ajax, $.isArray, $.fn.animate etc. In 2022 either better built-in solutions exist or better specialized tiny libraries exist. Everything that is supported by both Zepto and Cash should either work identically or Cash's implementation should be closer to…

Umbrella JS maintainer here (the library this HN thread is supposed to be about). At 2.8kb gzipped UmbrellaJS doesn't support partial builds because that'd be a bit funny. Also, and it seems to be something the author of the article likes a lot, Umbrella JS is closer to Javascript than the libraries you mention (& jQuery). Sure that means you need a bit of editing your code (which is made easy by being able to load b…

I didn't mean to hijack the thread, I was just replying to somebody saying something about Cash :)

I'm ambivalent regarding your documentation point, on one end jQuery is way more popular and one can largely just read jQuery's documentation for the methods that Cash implements too. On the other end that doesn't quite work when Cash's implementation works a bit differently than jQuery's, and just having a consistent, good, documentation is better in that case, and probably in general too.

I don't personally care much which libraries people use for their needs, personally I'm trying to incrementally move away to Cash and other jQuery-like direct DOM manipulation libraries entirely, but it takes time.

Re: Replacing jQuery (110kb) With UmbrellaJS (8kb)

#15
I generally replace jQuery with something like:

    $ = (s, p = document) => p.querySelector(s)
    $$ = (s, p = document) => p.querySelectorAll(s)
If I’m feeling arrayish I’ll wrap the latter in [...qSA] so I can use .map and the rest of them, otherwise for/of works just as well.

Anyone using $.animate() and $.ajax() this year should probably stop.

Re: Replacing jQuery (110kb) With UmbrellaJS (8kb)

#16
post #15

I generally replace jQuery with something like: $ = (s, p = document) => p.querySelector(s) $$ = (s, p = document) => p.querySelectorAll(s) If I’m feeling arrayish I’ll wrap the latter in [...qSA] so I can use .map and the rest of them, otherwise for/of works just as well. Anyone using $.animate() and $.ajax() this year should probably stop.

For some basic use cases that's probably good enough, but saying that that can replace jQuery is like saying that farts can replace rocket engines.

For example I can do the following with jQuery:

    $(document).on ( 'click.ns', '.button', callback );
How should one do the same with vanilla JS? You'd end up rewriting a (probably buggy) reimplementation of jQuery's $.fn.on method in the end.

Re: Replacing jQuery (110kb) With UmbrellaJS (8kb)

#18
post #10
post #9

Earlier quoted context omitted.

The author mentions that enforces CORS restrictions which would break with their current server settings and CDN, otherwise I think they agree with you.

Hmm, I think they should fix the CDN? I think if they just refer their script e.g. from https://www.jsdelivr.com/ it should just work? I have used bunch of modules from CDN.

The issue would lie with the site's CORS policy, no change to the CDN can work around that. And it's possible the author is not permitted to or does not wish to alter the CORS policy.

Re: Replacing jQuery (110kb) With UmbrellaJS (8kb)

#19

I wonder if w3c or whatwg guys considered extending dom spec to support the query / navigation / css helpers.

They have to an extent. `querySelector` / `querySelectorAll` is the clearest example, but many API's presented here came after jQuery and were inspired by it: https://youmightnotneedjquery.com/
Post reply on HN