framework_benchmark.rs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 std::rc::Rc;
  9. use dioxus::events::on::MouseEvent;
  10. use dioxus_core as dioxus;
  11. use dioxus_core::prelude::*;
  12. use dioxus_web::WebsysRenderer;
  13. fn main() {
  14. wasm_logger::init(wasm_logger::Config::new(log::Level::Debug));
  15. console_error_panic_hook::set_once();
  16. log::debug!("starting!");
  17. wasm_bindgen_futures::spawn_local(WebsysRenderer::start(App));
  18. }
  19. // We use a special immutable hashmap to make hashmap operations efficient
  20. type RowList = im_rc::HashMap<usize, Rc<str>, nohash_hasher::BuildNoHashHasher<usize>>;
  21. static App: FC<()> = |cx| {
  22. let (items, set_items) = use_state_classic(&cx, || RowList::default());
  23. let (selection, set_selection) = use_state_classic(&cx, || None as Option<usize>);
  24. let create_rendered_rows = move |from, num| move |_| set_items(create_row_list(from, num));
  25. let append_1_000_rows =
  26. move |_| set_items(create_row_list(items.len(), 1000).union(items.clone()));
  27. let update_every_10th_row = move |_| {
  28. let mut new_items = items.clone();
  29. let mut small_rng = SmallRng::from_entropy();
  30. new_items
  31. .iter_mut()
  32. .step_by(10)
  33. .for_each(|(_, val)| *val = create_new_row_label(&mut small_rng));
  34. set_items(new_items);
  35. };
  36. let clear_rows = move |_| set_items(RowList::default());
  37. let swap_rows = move |_| {
  38. // this looks a bit ugly because we're using a hashmap instead of a vec
  39. if items.len() > 998 {
  40. let mut new_items = items.clone();
  41. let a = new_items.get(&0).unwrap().clone();
  42. *new_items.get_mut(&0).unwrap() = new_items.get(&998).unwrap().clone();
  43. *new_items.get_mut(&998).unwrap() = a;
  44. set_items(new_items);
  45. }
  46. };
  47. let rows = items.iter().map(|(key, value)| {
  48. rsx!(Row {
  49. key: "{key}",
  50. row_id: *key as usize,
  51. label: value.clone(),
  52. })
  53. });
  54. cx.render(rsx! {
  55. div { class: "container"
  56. div { class: "jumbotron"
  57. div { class: "row"
  58. div { class: "col-md-6", h1 { "Dioxus" } }
  59. div { class: "col-md-6"
  60. div { class: "row"
  61. ActionButton { name: "Create 1,000 rows", id: "run", action: create_rendered_rows(0, 1_000) }
  62. ActionButton { name: "Create 10,000 rows", id: "runlots", action: create_rendered_rows(0, 10_000) }
  63. ActionButton { name: "Append 1,000 rows", id: "add", action: append_1_000_rows }
  64. ActionButton { name: "Update every 10th row", id: "update", action: update_every_10th_row, }
  65. ActionButton { name: "Clear", id: "clear", action: clear_rows }
  66. ActionButton { name: "Swap rows", id: "swaprows", action: swap_rows }
  67. }
  68. }
  69. }
  70. }
  71. table {
  72. tbody {
  73. {rows}
  74. }
  75. }
  76. span {}
  77. }
  78. })
  79. };
  80. #[derive(Props)]
  81. struct ActionButtonProps<F: Fn(Rc<dyn MouseEvent>)> {
  82. name: &'static str,
  83. id: &'static str,
  84. action: F,
  85. }
  86. fn ActionButton<F: Fn(Rc<dyn MouseEvent>)>(cx: Context<ActionButtonProps<F>>) -> VNode {
  87. cx.render(rsx! {
  88. div { class: "col-sm-6 smallpad"
  89. button {class:"btn btn-primary btn-block", 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(rng: &mut SmallRng) -> Rc<str> {
  118. let mut label = String::new();
  119. label.push_str(ADJECTIVES.choose(rng).unwrap());
  120. label.push(' ');
  121. label.push_str(COLOURS.choose(rng).unwrap());
  122. label.push(' ');
  123. label.push_str(NOUNS.choose(rng).unwrap());
  124. Rc::from(label)
  125. }
  126. fn create_row_list(from: usize, num: usize) -> RowList {
  127. let mut small_rng = SmallRng::from_entropy();
  128. (from..num + from)
  129. .map(|f| (f, create_new_row_label(&mut small_rng)))
  130. .collect::<RowList>()
  131. }
  132. static ADJECTIVES: &[&str] = &[
  133. "pretty",
  134. "large",
  135. "big",
  136. "small",
  137. "tall",
  138. "short",
  139. "long",
  140. "handsome",
  141. "plain",
  142. "quaint",
  143. "clean",
  144. "elegant",
  145. "easy",
  146. "angry",
  147. "crazy",
  148. "helpful",
  149. "mushy",
  150. "odd",
  151. "unsightly",
  152. "adorable",
  153. "important",
  154. "inexpensive",
  155. "cheap",
  156. "expensive",
  157. "fancy",
  158. ];
  159. static COLOURS: &[&str] = &[
  160. "red", "yellow", "blue", "green", "pink", "brown", "purple", "brown", "white", "black",
  161. "orange",
  162. ];
  163. static NOUNS: &[&str] = &[
  164. "table", "chair", "house", "bbq", "desk", "car", "pony", "cookie", "sandwich", "burger",
  165. "pizza", "mouse", "keyboard",
  166. ];