Add comparative benching with Sycamore reactivity

This commit is contained in:
Greg Johnston 2022-08-29 08:06:22 -04:00
parent 89d30d017c
commit b62233d45e
2 changed files with 95 additions and 2 deletions

View file

@ -11,6 +11,9 @@ elsa = "1"
wasm-bindgen = { version = "0.2", optional = true }
wasm-bindgen-futures = { version = "0.4", optional = true }
[dev-dependencies]
sycamore = "0.8.0-beta.7"
[features]
default = []
browser = ["dep:wasm-bindgen", "dep:wasm-bindgen-futures"]

View file

@ -51,10 +51,10 @@ mod tests {
use std::{cell::Cell, rc::Rc};
use crate::{create_effect, create_memo, create_scope, create_signal};
#[bench]
fn create_and_update_1000_signals(b: &mut Bencher) {
use crate::{create_effect, create_memo, create_scope, create_signal};
b.iter(|| {
create_scope(|cx| {
let acc = Rc::new(Cell::new(0));
@ -81,4 +81,94 @@ mod tests {
.dispose()
});
}
#[bench]
fn create_and_dispose_1000_scopes(b: &mut Bencher) {
use crate::{create_effect, create_scope, create_signal};
b.iter(|| {
let acc = Rc::new(Cell::new(0));
let disposers = (0..1000)
.map(|_| {
create_scope({
let acc = Rc::clone(&acc);
move |cx| {
let (r, w) = create_signal(cx, 0);
create_effect(cx, {
move |_| {
acc.set(r());
}
});
w(|n| *n += 1);
}
})
})
.collect::<Vec<_>>();
for disposer in disposers {
disposer.dispose();
}
});
}
#[bench]
fn sycamore_create_and_update_1000_signals(b: &mut Bencher) {
use sycamore::reactive::{create_effect, create_memo, create_scope, create_signal};
b.iter(|| {
let d = create_scope(|cx| {
let acc = Rc::new(Cell::new(0));
let sigs = Rc::new((0..1000).map(|n| create_signal(cx, n)).collect::<Vec<_>>());
let memo = create_memo(cx, {
let sigs = Rc::clone(&sigs);
move || sigs.iter().map(|r| *r.get()).sum::<i32>()
});
assert_eq!(*memo.get(), 499500);
create_effect(cx, {
let acc = Rc::clone(&acc);
move || {
acc.set(*memo.get());
}
});
assert_eq!(acc.get(), 499500);
sigs[1].set(*sigs[1].get() + 1);
sigs[10].set(*sigs[10].get() + 1);
sigs[100].set(*sigs[100].get() + 1);
assert_eq!(acc.get(), 499503);
assert_eq!(*memo.get(), 499503);
});
unsafe { d.dispose() };
});
}
#[bench]
fn sycamore_create_and_dispose_1000_scopes(b: &mut Bencher) {
use sycamore::reactive::{create_effect, create_scope, create_signal};
b.iter(|| {
let acc = Rc::new(Cell::new(0));
let disposers = (0..1000)
.map(|_| {
create_scope({
let acc = Rc::clone(&acc);
move |cx| {
let s = create_signal(cx, 0);
create_effect(cx, {
move || {
acc.set(*s.get());
}
});
s.set(*s.get() + 1);
}
})
})
.collect::<Vec<_>>();
for disposer in disposers {
unsafe {
disposer.dispose();
}
}
});
}
}