Rust is a language known for its speed
WASM is known to provide a secure, high performance runtime for the web
Typescript:
async function getUser(email: string): Promise<Result<Error, User>> {
const response = await fetch(`https://example.com/${email}`);
const user = await response.json();
return user.email === email
? Result.ok(user)
: Result.error(new Error('Incorrect user returned');
}
Rust:
async fn getUser(email: &str) -> Result<User, GetUserError> {
let user: User = get(format!("https://example.com/{email}")).await?
.json().await?;
if user.email == email {
Ok(user)
} else {
Err(GetUserError::IncorrectUserReturned)
}
}
#[function_component]
fn Counter() -> Html {
let counter = use_state(|| 0);
let increment = {
let counter = counter.clone();
move |_| { counter.set(*counter + 1); }
};
let decrement = {
let counter = counter.clone();
move |_| { counter.set(*counter - 1); }
};
html! {
<div>
<p>{ *counter }</p>
<button onclick={increment}>{ "+1" }</button>
<button onclick={decrement}>{ "-1" }</button>
</div>
}
}
#[function_component]
fn App() -> Html {
html! {
<>
<h1>{"Counter Example - Yew"}</h1>
<Counter />
</>
}
}
#[component]
#[auto_scope]
fn Counter<G: Html>(cx: Scope, state: &CounterStateRx) -> View<G> {
let increment = move |_| { state.total.set(*state.total.get() + 1) };
let decrement = move |_| { state.total.set(*state.total.get() - 1) };
view! { cx,
div {
p { (state.total.get()) }
button( on:click = increment}) { "+1" }
button( on:click = decrement) { "-1" }
}
}
}
#[derive(Default, Serialize, Deserialize, Clone, ReactiveState)]
#[rx(alias = "CounterStateRx")]
struct State {
total: i32,
}
#[auto_scope]
fn index_page<G: Html>(cx: Scope, state: &CounterStateRx) -> View<G> {
view! { cx,
h1 { "Counter Example - Perseus" }
Counter(state)
}
}
fn Counter(cx: Scope) -> Element {
let mut count = use_state(cx, || 0);
render!(
p { "{count}" }
button { onclick: move |_| count += 1, "+1" }
button { onclick: move |_| count -= 1, "-1" }
)
}
fn App(cx: Scope) -> Element {
render!(
h1 { "Counter Example - Dioxus" }
Counter { }
)
}
Name | dioxus v0.4.0 | sycamore v0.8.0 | react v18.2.0 | yew v0.20.0 |
---|---|---|---|---|
create rows | 39.7ms | 45.2ms | 45.6ms | 69.4ms |
replace all rows | 43.1ms | 49.8ms | 48.4ms | 73.3ms |
partial update | 83.6ms | 90.5ms | 103.2ms | 100.0ms |
select row | 13.4ms | 17.7ms | 24.1ms | 21.6ms |
swap rows | 26.8ms | 26.2ms | 160.7ms ⚠️ | 27.0ms |
remove row | 40.9ms | 41.3ms | 43.5ms | 42.1ms |
create many rows | 433.3ms | 569.3ms | 619.2ms | 2,386.9ms ⚠️ |
append rows to large table | 91.8ms | 100.5ms | 99.5ms | 153.8ms |
clear rows | 32.8ms | 33.0ms | 30.7ms | 52.0ms |
geometric mean of all factors in the table | 1.16 | 1.29 | 1.67 | 1.90 |
Results from js-framework-benchmark