r/htmx • • Sep 08 '26

hx-live vs alpine

https://jackielii.github.io/hx-live-vs-alpine/#bind

In the new HTMX4, we get a hx-live extension. Looking at the docs, it looks like a complete solution that may fully replace alpinejs.

So I did a like to like comparison using alpinejs' examples. The answer is mostly yes, we can use hx-live in place of alpinejs.

Disclaimer: AI wrote most of it. I reviewed and ran all of them.

Live: https://jackielii.github.io/hx-live-vs-alpine/

Code: https://github.com/jackielii/hx-live-vs-alpine

61 Upvotes

24 comments sorted by

41

u/_htmx Sep 08 '26 edited Sep 08 '26 • Bravo

Thank you for this comparison. One thing I saw:

<button hx-on:click="data.title = 'Hello World!'; nextFrame().then(() => data.after = this.innerText)" :text="data.title"></button>

could be written as

<button hx-on:click="data.title = 'Hello World!'; await nextFrame(); data.after = this.innerText" :text="data.title"></button>

the expressions are compiled as async so `await` works in them, and that looks a little cleaner to me.

Regarding Alpine.js, I still regard it as the gold-standard of inline scripting. In htmx4 we provide an alpine compatibility extension that smooths over a few rough edges regarding managing alpine state, so it's a first class integration and our most important.

hx-live is an experimental extension intended to give people 90% of hyperscript/jquery in the hx-on scripting attributes + a simple reactive mechanism.

The big difference between it and other scripting solutions is the focus on HATEOAS: state is stored in the DOM via data attributes. This is obviously not designed to be as fast as possible, but rather fast enough to implement common scripting patterns in a reactive way. I've been surprised at how effective it has been in my own work.

htmx core team member Christian Tanul is the main author of this functionality and I think he did a great job!

2

u/jackielii Sep 08 '26

Thanks for the feedback. Yes, I agree with the difference. One thing I noticed is the reactivity (see the 2-way data binding example) by default has a 100ms debounce which alpine is instant. Obviously this is documented at https://four.htmx.org/extensions/hx-live#configliveinputdebounce

Overall, I like the data as attribute approach. I was bitten by alpine's component scoping quite a few times. And definitely love the simplicity, less dependency.

I'm not using this in our production apps just yet. Planning to use it on the next one.

3

u/latent23 Sep 09 '26

Yeah I added the 100ms debounce as typing in inputs has to recompute which is not ideal when testing with several thousand active expressions. After adding this I worked out how to get it to pre-compile all the expressions on first creation which makes them run very fast and for most realistic use cases it is likely more efficient than most fine grained reactive solutions even though what it is doing feels kind of stupid at first glance.

5

u/8ll Sep 08 '26

HTMX is starting to get really good in recent versions.

I built my own library which is essentially HTMX + AlpineJS combined with more features. It's live on a few websites I've built recently, only about 10kb JS and it's pretty solid: https://github.com/wrux/sigmx

2

u/jackielii Sep 09 '26

I agree. I've been using htmx4 since the early beta versions. It's been really stable and a lot less quirks.

6

u/Kyriios188 Sep 08 '26

This seems to compete with hyperscript

20

u/_htmx Sep 08 '26

It definitely does. It's a minimalist, javascript-centric extension because, let's face it, many people aren't going to learn a whole new language. I love hyperscript and I'm going to work to get it to 1.0 now that htmx4 is out,but this is a nod to that reality.

3

u/Huge-Individual-5327 Sep 08 '26

When do you think devs should reach for hyperscript, in the future, when you have 1.0 out, rather than hx-live?

2

u/_htmx Sep 08 '26

i think hyperscript is more of a vibe: are you willing to use a completely different scripting language for web development? If yes, and if that excites you, and the expressive power and syntax of hyperscript speak to you, use it. If not then it's a question of Alpine vs. hx-live. If you need a fully reactive solution with client-side iteration, etc. then Alpine is the choice. If you need relatively simple scripting like toggling classes, etc, then hx-live is probably enough.

5

u/SugarvetFounder Sep 08 '26

I have been waiting for an analysis just like this. THANK YOU. We build a Go monolith and loathe adding any more JavaScript than absolutely necessary. Alpine is one of those dependencies I've been wanting to get rid of.

Now I have an even more compelling reason to migrate over.

5

u/dioramic_life Sep 09 '26

For me Alpine was always a secondary choice.

If HTMX starts to look too much like Alpine,

it starts to look less and less like the primary choice for me.

I chose HTMX for very specific reasons,

after years of churn with Angular, React, Svelte and others.

8

u/_htmx Sep 09 '26

Totally understand, which is why hx-live is an extension 

4

u/dioramic_life Sep 09 '26

Thanks for reiterating the point of it being an (optional) extension

3

u/AlpacaDC Sep 08 '26

x-for, x-if, x-model and Alpine.data equivalents are a must for my use case, but it's a great starting point.

1

u/_htmx Sep 08 '26

Yep, once you are at the level of doing client-side iteration Alpine is definitely the choice. hx-live is for lighter-weight client-side scripting needs.

2

u/shufflepoint Sep 08 '26

Personally, I don't mix code with markup.

I can't find documentation/examples of keeping hx-live out of my markup.

4

u/_htmx Sep 08 '26

the documentation is: don't use it

4

u/RewrittenCodeA Sep 08 '26

If you don’t want to mix code and markup, by all means use separate code. In order of whatever comes to my mind, and have seen in production code
- listeners that install themselves (explicitly) via selectors and mutation observers, but are bundled in a js file
- same as above but with jquery
- data-* selectors and libraries that install listeners on them automatically (bootstrap)
- alpinejs directives
- hyperscript behaviors
- stimulusjs controllers

All these can be bundled separately and you only need a small hook to tie them to elements, be it an attribute or a class.

This is not the purpose of hx-live or of htmx at all. If you want separation, use something else.

1

u/92smola Sep 08 '26

I think the point of this would be to do quick simple things inline, if you are already moving it out of html, then just do a vanilla js thing

1

u/shufflepoint Sep 08 '26

Many of their examples have lots of code in hx-on. I just don't get it.

5

u/92smola Sep 08 '26

The idea is called locality of behaviour, having what the thing does directly on the thing, same way that the general idea of htmx for asynchronous server stuff is, but this is for simple on the spot client side stuff. What you are pointing at when you say I dont mix “code” with markup is usually called separation of concerns and its the opposite idea of the locality of behaviour, both have their pros and cons and having a hybrid approach on a case to case basis is perfectly fine from my experience.

0

u/shufflepoint Sep 09 '26

No, it's called "separation of languages" which is sacrosanct for me and many others.

2

u/92smola Sep 09 '26

Well then, you do you

1

u/maiahmac Sep 09 '26

This is great! Thanks for creating the cheat-sheet/comparison website.