, which means we can keep the
same DOM node
We enumerate all the attributes on the old
and the new one to see if any need to be
changed, added or removed. In both cases we have a single attribute — a className with a
value of "greeting"
Descending into the element, we see that the text has changed, so we'll need to update the
real DOM
Of these three steps, only the third has value in this case, since — as is the case in the
vast majority of updates — the basic structure of the app is unchanged. It would be much
more efficient if we could skip straight to step 3:
if (changed.name) {
text.data = name;
}
(This is almost exactly the update code that Svelte generates. Unlike traditional UI
frameworks, Svelte is a compiler that knows at build time how things could change in your
app, rather than waiting to do the work at run time.)
Unnecessary work
function MoreRealisticComponent(props) {
const [selected, setSelected] = useState(null);
return (
Selected {selected ? selected.name : 'nothing'}
{props.items.map(item =>
{item.name}
)}
);
}
Here, we're generating a new array of virtual <li> elements
— each with their own inline event handler —
on every state change, regardless of whether props.items has changed.
Unless you're unhealthily obsessed with performance,
you're not going to optimise that. There's no point. It's plenty fast enough. But you know what
would be even faster? Not doing that.
The danger of defaulting to doing unnecessary work, even if that work is trivial, is that your app
will eventually succumb to 'death by a thousand cuts' with no clear bottleneck to aim at once it's
time to optimise.
Svelte is explicitly designed to prevent you from ending up in that situation.
Why do frameworks use the virtual DOM then?
It's important to understand that virtual DOM isn't a feature.
It's a means to an end, the end being declarative, state-driven UI development.
Virtual DOM is valuable because it allows you to build apps without thinking about state
transitions, with performance that is generally good enough.
That means less buggy code, and more time spent on creative tasks instead of tedious ones.
But it turns out that we can achieve a similar programming model without using virtual DOM — and
that's where Svelte comes in.
가상 DOM은 기능이 아니라는 것을 이해하는 것이 중요하다. 그것은 목적을 위한 수단이며, 목적은 선언적이고, 주 주도적인 UI 개발이다.
가상 DOM은 일반적으로 충분한 성능을 가지고 상태 전환에 대해 생각하지 않고 앱을 구축할 수 있기 때문에 가치가 있다.
이것은 버그 코드가 덜하고 지루한 작업 대신에 창의적인 작업에 더 많은 시간을 소비한다는 것을 의미한다.
So, What is Svelte?
Svelte is a component framework — like React or Vue
But,
there's an important difference
Traditional frameworks allow you to write declarative state-driven code
Penalty: the browser must do extra work to convert those declarative structures into DOM
operations, using techniques like that eat into your frame budget and tax the garbage collector.
Svelte runs at BUILD TIME
converting your components into
highly efficient imperative code that surgically updates the DOM
As a result, you're able to write
ambitious applications with excellent performance
characteristics
Let's see the performance examples
Result of memory usage
React : 30 ~ 110 MB
Svelte: 15 ~ 30 MB
SVELTE has the fantastic performance
Let's see the Svelte Syntax
Reactive
Any top-level statement (i.e. not inside a block or a function) can be made reactive by prefixing it
with the $: .
Reactive statements run immediately before the component updates, whenever the values that they
depend on have changed.
Store
// The svelte/store module contains minimal store implementations which fulfil this contract.
// You can use these as the basis for your own stores, or you can implement your stores from scratch.
const unsubscribe = store.subscribe(value => {
console.log(value);
}); // logs `value`
// later...
unsubscribe();
import { writable } from 'svelte/store';
const count = writable(0);
console.log($count); // logs 0
count.set(1);
console.log($count); // logs 1
$count = 2;
console.log($count); // logs 2
// https://svelte.dev/docs#svelte_store
{#if ...}
{#if porridge.temperature > 100}
too hot!
{:else if 80 > porridge.temperature}
too cold!
{:else}
just right!
{/if}
{#each...}
Shopping list
{#each items as item}
{item.name} x {item.qty}
{/each}
on:eventname
count: {count}
Immutable
Now, when you toggle todos by clicking on them, only the updated component flashes.
Don't you think this is kind of cool stuff?😎
SVELTE is specially stand for ...
Embedded Web & Mobile Web
웹앱으로 포스단말기 UI를 제작하는 경우에 svelte를 사용
https://www.theengineeringprojects.com/wp-content/uploads/2016/11/Examples-of-Embedded-Systems.jpg
SVELTE satisfied those of features
Minimized Javascript&CSS bundling(5 ~ 20KB)
Extremely high performance
Easy to learn
Rapid web app development
Beyond SVELTE
SAPPER: Next/Gatsby-style framework. Automic SSR & Code-splitting
SVELTE NATIVE: Based on nativescript-vue
SVELTE GL: Like Three.js
Generally support typescript. But... we can fix syntax errors at the build time.
How about starting development using SVELTE ?
We're hiring!
bit.ly/dangguen
Thank you!!
Email: novemberde1@gmail.com
Blog: https://novemberde.github.io
Github: https://github.com/novemberde