Skip to content
loading...

Svelte Needs A Virtual DOM

Vani Shivanand on January 09, 2020

For decades, we were working without a Virtual DOM and it was accepted widely in the last decade soon after it was introduced mainly because the be... [Read Full]
markdown guide
 

Hey, I just created a small snippet to explain better the approach Svelte uses to update the dom:

That's a Svelte version, you can check using devtools:
svelte.dev/repl/567089d42c3b414682...

That's a vanilla version to have a big overview of how Svelte schedule the updates:
jsfiddle.net/n4h7s56y

You can see that it doesn't update the dom multiple times!

The main difference with React will be when the concurrent mode is released. React will have a way to "stop and continue" the rendering process between the cycles/ticks.

But, actually Svelte uses a completely different approach, and I'm not saying it's better or worst. It's just different. I really like React and I'm impressed with Rich Harris (and the community) work on Svelte. I'm pretty sure we have space for both.

The other thing is: actually, if we run, e.g:

(a, b) => a + b

and

() => {}

Which one will run faster? jsperf.com/sum-x-empty
So, if you remove the code, of course it will be faster, and less code, less bugs.

Of course, it's not the only way you can improve the speed of your app, but it's the only one that it's 100% guaranteed it will do.

And about React, well, I think they are thinking too much of how to make the things faster, run smooth, but, maybe would be good to think like Rich Harris? Instead of trying new ways to speed up, just remove the code, make ReactDOM bundle smaller, maybe tree-shakable, less boilerplate on the events layer, or even, invest on an ahead of time compilation.

So, my 2cents: Svelte don't need a virtual DOM. :)

Some useful links about the topic:
// some links talking about the virtual dom (opinionated maybe? go for your own risk, haha)
svelte.dev/blog/virtual-dom-is-pur...
twitter.com/thekitze/status/107858...

// real world implementation in svelte an react+redux
github.com/sveltejs/realworld
github.com/gothinkster/react-redux...

// compile time computations to js to minimize the work in runtime
prepack.io/

// build react to html in compile time
twitter.com/trueadm/status/1002812...
github.com/trueadm/ssr-with-prepac...

 

Thanks for the comment.

I didn't mean one line update in the latter example. It's supposed to be this

let number = 1;
number++;
number=+2;
number=+3;
let displayNumber = number

So, the empty comparison doesn't hold good.

Trying new ways to speed up is good. Svelte is an awesome framework. But react is a lot mature and handles more scenarios to optimize performance.

I could see layout taking a lot more time in Svelte which is also a major concern.

I will also make more study on the same and update the same on data basis next time.

 

React does not handle more scenarios.

React exposes more hooks or states so you can inform it how to be more performant.

ShouldComponentUpdate, ShouldComponentMount, or what have you. It’s been a while.

The reality is, you should not need those, if it was not for a systemic performance issue. Look at 2016 articles of optimizing React before react 16. Sure it got better since, but so has our devices, internet and browsers.

Run that code in a nexus 5 with a 3G connection in Puerto Rico inside a Facebook webview.

Sorry to differ it does handle more scenarios. One of them is, it compares the dom and then updates. A read is much faster than the write as write has to alter the entire dom tree.

Your missing my point.

It does not matter the library does more work. What matters is the speed for the user. It’s great they do that. But you are downloading a much larger bundle size to get that benefit. And in 95 percent of SPA sites, it’s not needed.

When we talk about performance, we have a habit of saying “this is faster” or “this updates more elements” or “it’s smarter about how it does it”.

The reality is, touching the DOM is not bad. And in most cases the added benefit of a virtual DOM is not felt. And Svelte performance actually shows its faster without the virtual DOM than react is with it.

This will keep going back and forth as both libraries keep optimizing. The big thing here, is to understand that a virtual dom is not a requirement for a fast experience. And the cost of it, is a larger bundle size.

When you remove the developer experience, and your look at the performance realistically (perception and what is needed) your left with bundle size. And react can’t touch svelte on that.

The whole point is updating the Dom is 100% slower. Please have a look at the results here, dev.to/svaani/is-virtual-dom-neede...

Ok at this point your not really reading or consuming other viewpoints.

Tests I have seen that are not yours have shown otherwise, but please read everything else I said about performance.

I would also say the same, you are not reading all the points that I say.

Yes svelte is a lot better for not complex apps.

If you say 95% of the websites don't need that, we must see most percent of websites are wordpress without a thought on optimization.

Anyway, please have a look at this.

dev.to/svaani/svelte-vs-reactjs-pe...

 

I feel like you are looking at this wrong...

There are two things that matter.

  1. Bundle Size
  2. Performance that is realistic to your app

We say everything needs a virtual dom, then we build a website with React that does not need reactivity. Looking at almost every marketing site ever.

So I choose svelte because of the lower bundle size.

Then we say it performs better, the virtual dom. Well, most performance tests I have seen show the opposite. That svelte out performs virtual dom libraries.

For this argument I look too scale. The stress cases have thousands and thousands of things being updated. My web app does not, so this does not seem to matter much.

So once again, back to bundle size, and I choose svelte.

I appreciate the technical argument and the debate. But we all seem to forget that most of the web is Wordpress and jQuery still. That vanilla HTML, CSS and JS solve most of our issues and we are debating developer experience not user experience.

Start with the user experience, you won’t go wrong. But don’t claim performance issues without a actual user context and impact. And don’t sacrifice bundle size for a few extra frames a user can’t decern because you think it’s neat, or want to defend your technology choices.

 

Svelte performs a lot better when the app is small. Imagine a live updating website or Facebook being developed in svelte.

Bundle size does matter but as the application grows, all the functionalities in it are a need.

 

Where do you see proof that svelte can’t handle something like Facebook. That is just assumptions. Facebook was built without a virtual dom long before it had one.

And the difference is the size the JavaScript is to perform the same actions for the user. You can’t claim that it’s ok to have a larger bundle size and it’s justified because of react because of functionality svelte or even just HTML, css, and JS can’t do. React isn’t magic.

Also, once again most websites are not the size of Facebook. So it’s about your needs.

I would also argue it’s about how the developers work offline, the component architecture, that makes these frameworks valuable. Not the user experience. Nothing a SPA, svelte or react, does is something a standard site can’t do from a users point of view.

 

You are talking about a batching mechanism that has nothing to do with the virtual DOM. Like look at MobX which uses the concept of actions to batch changes. The only reason the Virtual DOM doesn't update in this case is default behavior is to batch. But if you wanted it to change independently you could. And with most reactive libraries (I can't speak for Svelte) you can batch changes. Has nothing to do with the Virtual DOM. Virtual DOM can be very fast so I don't want people to buy into the Svelte hype. There are way faster VDOM libraries than Svelte. There are compiled reactive libraries way faster than those too. But React isn't even close.

Don't confuse batching with the Virtual DOM.

 

Hi Vani, Svelte will check what changed in the next microtask and only then apply DOM updates. So Svelte will update the DOM just once, not four times. You could look at the generated code on svelte.dev/repl

 

Yes, but the four times is used for the simplified explanation. It's about updating large chunks of data. May be I was not very clear in my explaination. I shall re-quote it with better examples or better words.

 

I am really curious to see actual data, because the arguments you list here seem wrong (svelte does not update 4 times either, as I see it) or do not convince me. In contrast, I perceive the virtual DOM for most tasks an unnecessary overhead. Only the really big/intensive applications like Fakebook, where you have hundrets of scripts all loading and modifying the DOM simultaneously seem like a valid use case to me.

 

Yes, it was for the simplified illustration. May be, I wasn't clear in my words. I meant to say if "four changes" happen. I will upload the reports for the actual data.

 

This is so wrong, angular and svelte framework will seek to the models changes after the next cycle.

The framework will not magically listen to any var change and commit to the dom as it happens.

 

Yes that's right. But I am speaking about large chunks of data. Here it was only for the simplified example, I used four times update.

 

Angularjs is still big due to all the legacy's apps. In 2020 I would not use it as a comparison

 

Both Svelte and React end up making the exact same number of changes to the DOM. Even for large data changes. The only difference is that react reconciles the changes by using the virtual DOM first. Svelte updates the DOM based on what was changed usinh the compile time knowledge of how to make said change. The only place react might be faster is adding a huge number of nodes in a very short time as it could decide to use something like innerHTML to get all the elements on screen and then hydrate them next tick. If you're adding 50+ nodes in a single cycle on a regular basis, then you probably have a problem. Even Facebook doesn't update that many things at once, why? Because it's bad practice and you should be focused on reacting to user interactions.

 

Oh, and about Angular and Vue, I actually don't know about the internals of both.

And just throwing another interesting link:
freecodecamp.org/news/the-virtual-... (Imba Memo DOM, I personally never used, haha).

 
code of conduct - report abuse