jsframework.rs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. //! This benchmark tests just the overhead of Dioxus itself.
  2. //!
  3. //! For the JS Framework Benchmark, both the framework and the browser is benchmarked together. Dioxus prepares changes
  4. //! to be made, but the change application phase will be just as performant as the vanilla wasm_bindgen code. In essence,
  5. //! we are measuring the overhead of Dioxus, not the performance of the "apply" phase.
  6. //!
  7. //! On my MBP 2019:
  8. //! - Dioxus takes 3ms to create 1_000 rows
  9. //! - Dioxus takes 30ms to create 10_000 rows
  10. //!
  11. //! As pure "overhead", these are really really good numbers, mostly slowed down by hitting the global allocator.
  12. //! These numbers don't represent Dioxus with the heuristic engine installed, so I assume it'll be even faster.
  13. use std::fmt::Display;
  14. use criterion::{black_box, 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().unwrap();
  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. cx.render(rsx! {
  56. tr {
  57. td { class:"col-md-1", "{cx.row_id}" }
  58. td { class:"col-md-1", onclick: move |_| { /* run onselect */ }
  59. a { class: "lbl", "{cx.label}" }
  60. }
  61. td { class: "col-md-1"
  62. a { class: "remove", onclick: move |_| {/* remove */}
  63. span { class: "glyphicon glyphicon-remove remove" aria_hidden: "true" }
  64. }
  65. }
  66. td { class: "col-md-6" }
  67. }
  68. })
  69. }
  70. #[derive(PartialEq)]
  71. struct Label([&'static str; 3]);
  72. impl Label {
  73. fn new(rng: &mut SmallRng) -> Self {
  74. Label([
  75. ADJECTIVES.choose(rng).unwrap(),
  76. COLOURS.choose(rng).unwrap(),
  77. NOUNS.choose(rng).unwrap(),
  78. ])
  79. }
  80. }
  81. impl Display for Label {
  82. fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
  83. write!(f, "{} {} {}", self.0[0], self.0[1], self.0[2])
  84. }
  85. }
  86. static ADJECTIVES: &[&str] = &[
  87. "pretty",
  88. "large",
  89. "big",
  90. "small",
  91. "tall",
  92. "short",
  93. "long",
  94. "handsome",
  95. "plain",
  96. "quaint",
  97. "clean",
  98. "elegant",
  99. "easy",
  100. "angry",
  101. "crazy",
  102. "helpful",
  103. "mushy",
  104. "odd",
  105. "unsightly",
  106. "adorable",
  107. "important",
  108. "inexpensive",
  109. "cheap",
  110. "expensive",
  111. "fancy",
  112. ];
  113. static COLOURS: &[&str] = &[
  114. "red", "yellow", "blue", "green", "pink", "brown", "purple", "brown", "white", "black",
  115. "orange",
  116. ];
  117. static NOUNS: &[&str] = &[
  118. "table", "chair", "house", "bbq", "desk", "car", "pony", "cookie", "sandwich", "burger",
  119. "pizza", "mouse", "keyboard",
  120. ];