framework_benchmark.rs 5.5 KB

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