framework_benchmark.rs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. //! JS Framework Benchmark
  2. //! ----------------------
  3. //!
  4. //! This example is used in the JS framework benchmarking tool to compare Dioxus' performance with other frontend frameworks.
  5. //!
  6. //!
  7. //!
  8. use dioxus::{events::on::MouseEvent, prelude::*};
  9. use dioxus_html as dioxus_elements;
  10. use fxhash::{FxBuildHasher, FxHasher32};
  11. use std::rc::Rc;
  12. fn main() {
  13. log::debug!("starting!");
  14. dioxus::desktop::launch(App, |c| c);
  15. }
  16. // We use a special immutable hashmap to make hashmap operations efficient
  17. type RowList = im_rc::HashMap<usize, Rc<str>, FxBuildHasher>;
  18. static App: FC<()> = |cx| {
  19. let items = use_state(cx, || RowList::default());
  20. let create_rendered_rows = move |from, num| move |_| items.set(create_row_list(from, num));
  21. let append_1_000_rows =
  22. move |_| items.set(create_row_list(items.len(), 1000).union((*items).clone()));
  23. let update_every_10th_row = move |_| {
  24. let mut new_items = (*items).clone();
  25. let mut small_rng = SmallRng::from_entropy();
  26. new_items.iter_mut().step_by(10).for_each(|(_, val)| {
  27. *val = create_new_row_label(&mut String::with_capacity(30), &mut small_rng)
  28. });
  29. items.set(new_items);
  30. };
  31. let clear_rows = move |_| items.set(RowList::default());
  32. let swap_rows = move |_| {
  33. // this looks a bit ugly because we're using a hashmap instead of a vec
  34. if items.len() > 998 {
  35. let mut new_items = (*items).clone();
  36. let a = new_items.get(&0).unwrap().clone();
  37. *new_items.get_mut(&0).unwrap() = new_items.get(&998).unwrap().clone();
  38. *new_items.get_mut(&998).unwrap() = a;
  39. items.set(new_items);
  40. }
  41. };
  42. let rows = items.iter().map(|(key, value)| {
  43. rsx!(Row {
  44. key: "{key}",
  45. row_id: *key as usize,
  46. label: value.clone(),
  47. })
  48. });
  49. cx.render(rsx! {
  50. div { class: "container"
  51. div { class: "jumbotron"
  52. div { class: "row"
  53. div { class: "col-md-6", h1 { "Dioxus" } }
  54. div { class: "col-md-6"
  55. div { class: "row"
  56. ActionButton { name: "Create 1,000 rows", id: "run", action: create_rendered_rows(0, 1_000) }
  57. ActionButton { name: "Create 10,000 rows", id: "runlots", action: create_rendered_rows(0, 10_000) }
  58. ActionButton { name: "Append 1,000 rows", id: "add", action: append_1_000_rows }
  59. ActionButton { name: "Update every 10th row", id: "update", action: update_every_10th_row, }
  60. ActionButton { name: "Clear", id: "clear", action: clear_rows }
  61. ActionButton { name: "Swap rows", id: "swaprows", action: swap_rows }
  62. }
  63. }
  64. }
  65. }
  66. table {
  67. tbody {
  68. {rows}
  69. }
  70. }
  71. span {}
  72. }
  73. })
  74. };
  75. #[derive(Clone)]
  76. struct RowController {}
  77. #[derive(Props)]
  78. struct ActionButtonProps<F: Fn(MouseEvent)> {
  79. name: &'static str,
  80. id: &'static str,
  81. action: F,
  82. }
  83. fn ActionButton<F>(cx: Context<ActionButtonProps<F>>) -> VNode
  84. where
  85. F: Fn(MouseEvent),
  86. {
  87. cx.render(rsx! {
  88. div { class: "col-sm-6 smallpad"
  89. button { class:"btn btn-primary btn-block", r#type: "button", id: "{cx.id}", onclick: {&cx.action},
  90. "{cx.name}"
  91. }
  92. }
  93. })
  94. }
  95. #[derive(PartialEq, Props)]
  96. struct RowProps {
  97. row_id: usize,
  98. label: Rc<str>,
  99. }
  100. fn Row<'a>(cx: Context<'a, RowProps>) -> VNode {
  101. cx.render(rsx! {
  102. tr {
  103. td { class:"col-md-1", "{cx.row_id}" }
  104. td { class:"col-md-1", onclick: move |_| { /* run onselect */ }
  105. a { class: "lbl", "{cx.label}" }
  106. }
  107. td { class: "col-md-1"
  108. a { class: "remove", onclick: move |_| {/* remove */}
  109. span { class: "glyphicon glyphicon-remove remove" aria_hidden: "true" }
  110. }
  111. }
  112. td { class: "col-md-6" }
  113. }
  114. })
  115. }
  116. use rand::prelude::*;
  117. fn create_new_row_label(label: &mut String, rng: &mut SmallRng) -> Rc<str> {
  118. label.push_str(ADJECTIVES.choose(rng).unwrap());
  119. label.push(' ');
  120. label.push_str(COLOURS.choose(rng).unwrap());
  121. label.push(' ');
  122. label.push_str(NOUNS.choose(rng).unwrap());
  123. Rc::from(label.as_ref())
  124. }
  125. fn create_row_list(from: usize, num: usize) -> RowList {
  126. let mut small_rng = SmallRng::from_entropy();
  127. let mut buf = String::with_capacity(35);
  128. (from..num + from)
  129. .map(|f| {
  130. let o = (f, create_new_row_label(&mut buf, &mut small_rng));
  131. buf.clear();
  132. o
  133. })
  134. .collect::<RowList>()
  135. }
  136. static ADJECTIVES: &[&str] = &[
  137. "pretty",
  138. "large",
  139. "big",
  140. "small",
  141. "tall",
  142. "short",
  143. "long",
  144. "handsome",
  145. "plain",
  146. "quaint",
  147. "clean",
  148. "elegant",
  149. "easy",
  150. "angry",
  151. "crazy",
  152. "helpful",
  153. "mushy",
  154. "odd",
  155. "unsightly",
  156. "adorable",
  157. "important",
  158. "inexpensive",
  159. "cheap",
  160. "expensive",
  161. "fancy",
  162. ];
  163. static COLOURS: &[&str] = &[
  164. "red", "yellow", "blue", "green", "pink", "brown", "purple", "brown", "white", "black",
  165. "orange",
  166. ];
  167. static NOUNS: &[&str] = &[
  168. "table", "chair", "house", "bbq", "desk", "car", "pony", "cookie", "sandwich", "burger",
  169. "pizza", "mouse", "keyboard",
  170. ];