jsframework.rs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #![allow(non_snake_case, non_upper_case_globals)]
  2. //! This benchmark tests just the overhead of Dioxus itself.
  3. //!
  4. //! For the JS Framework Benchmark, both the framework and the browser is benchmarked together. Dioxus prepares changes
  5. //! to be made, but the change application phase will be just as performant as the vanilla wasm_bindgen code. In essence,
  6. //! we are measuring the overhead of Dioxus, not the performance of the "apply" phase.
  7. //!
  8. //! On my MBP 2019:
  9. //! - Dioxus takes 3ms to create 1_000 rows
  10. //! - Dioxus takes 30ms to create 10_000 rows
  11. //!
  12. //! As pure "overhead", these are amazing good numbers, mostly slowed down by hitting the global allocator.
  13. //! These numbers don't represent Dioxus with the heuristic engine installed, so I assume it'll be even faster.
  14. use criterion::{criterion_group, criterion_main, Criterion};
  15. use dioxus_core as dioxus;
  16. use dioxus_core::prelude::*;
  17. use dioxus_html as dioxus_elements;
  18. use rand::prelude::*;
  19. criterion_group!(mbenches, create_rows);
  20. criterion_main!(mbenches);
  21. fn create_rows(c: &mut Criterion) {
  22. static App: FC<()> = |cx| {
  23. let mut rng = SmallRng::from_entropy();
  24. let rows = (0..10_000).map(|f| {
  25. let label = Label::new(&mut rng);
  26. rsx! {
  27. Row {
  28. row_id: f,
  29. label: label
  30. }
  31. }
  32. });
  33. cx.render(rsx! {
  34. table {
  35. tbody {
  36. {rows}
  37. }
  38. }
  39. })
  40. };
  41. c.bench_function("create rows", |b| {
  42. b.iter(|| {
  43. let mut dom = VirtualDom::new(App);
  44. let g = dom.rebuild();
  45. assert!(g.edits.len() > 1);
  46. })
  47. });
  48. }
  49. #[derive(PartialEq, Props)]
  50. struct RowProps {
  51. row_id: usize,
  52. label: Label,
  53. }
  54. fn Row<'a>(cx: Context<'a, RowProps>) -> DomTree {
  55. let [adj, col, noun] = cx.label.0;
  56. cx.render(rsx! {
  57. tr {
  58. td { class:"col-md-1", "{cx.row_id}" }
  59. td { class:"col-md-1", onclick: move |_| { /* run onselect */ }
  60. a { class: "lbl", "{adj}" "{col}" "{noun}" }
  61. }
  62. td { class: "col-md-1"
  63. a { class: "remove", onclick: move |_| {/* remove */}
  64. span { class: "glyphicon glyphicon-remove remove" aria_hidden: "true" }
  65. }
  66. }
  67. td { class: "col-md-6" }
  68. }
  69. })
  70. }
  71. #[derive(PartialEq)]
  72. struct Label([&'static str; 3]);
  73. impl Label {
  74. fn new(rng: &mut SmallRng) -> Self {
  75. Label([
  76. ADJECTIVES.choose(rng).unwrap(),
  77. COLOURS.choose(rng).unwrap(),
  78. NOUNS.choose(rng).unwrap(),
  79. ])
  80. }
  81. }
  82. static ADJECTIVES: &[&str] = &[
  83. "pretty",
  84. "large",
  85. "big",
  86. "small",
  87. "tall",
  88. "short",
  89. "long",
  90. "handsome",
  91. "plain",
  92. "quaint",
  93. "clean",
  94. "elegant",
  95. "easy",
  96. "angry",
  97. "crazy",
  98. "helpful",
  99. "mushy",
  100. "odd",
  101. "unsightly",
  102. "adorable",
  103. "important",
  104. "inexpensive",
  105. "cheap",
  106. "expensive",
  107. "fancy",
  108. ];
  109. static COLOURS: &[&str] = &[
  110. "red", "yellow", "blue", "green", "pink", "brown", "purple", "brown", "white", "black",
  111. "orange",
  112. ];
  113. static NOUNS: &[&str] = &[
  114. "table", "chair", "house", "bbq", "desk", "car", "pony", "cookie", "sandwich", "burger",
  115. "pizza", "mouse", "keyboard",
  116. ];