jsframework.rs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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_core_macro::*;
  18. use dioxus_html as dioxus_elements;
  19. use rand::prelude::*;
  20. fn main() {}
  21. criterion_group!(mbenches, create_rows);
  22. criterion_main!(mbenches);
  23. fn create_rows(c: &mut Criterion) {
  24. static App: FC<()> = |(cx, _)| {
  25. let mut rng = SmallRng::from_entropy();
  26. let rows = (0..10_000_usize).map(|f| {
  27. let label = Label::new(&mut rng);
  28. rsx! {
  29. Row {
  30. row_id: f,
  31. label: label
  32. }
  33. }
  34. });
  35. cx.render(rsx! {
  36. table {
  37. tbody {
  38. {rows}
  39. }
  40. }
  41. })
  42. };
  43. c.bench_function("create rows", |b| {
  44. b.iter(|| {
  45. let mut dom = VirtualDom::new(App);
  46. let g = dom.rebuild();
  47. assert!(g.edits.len() > 1);
  48. })
  49. });
  50. }
  51. #[derive(PartialEq, Props)]
  52. struct RowProps {
  53. row_id: usize,
  54. label: Label,
  55. }
  56. fn Row((cx, props): Scope<RowProps>) -> Element {
  57. let [adj, col, noun] = props.label.0;
  58. cx.render(rsx! {
  59. tr {
  60. td { class:"col-md-1", "{props.row_id}" }
  61. td { class:"col-md-1", onclick: move |_| { /* run onselect */ }
  62. a { class: "lbl", "{adj}" "{col}" "{noun}" }
  63. }
  64. td { class: "col-md-1"
  65. a { class: "remove", onclick: move |_| {/* remove */}
  66. span { class: "glyphicon glyphicon-remove remove" aria_hidden: "true" }
  67. }
  68. }
  69. td { class: "col-md-6" }
  70. }
  71. })
  72. }
  73. #[derive(PartialEq)]
  74. struct Label([&'static str; 3]);
  75. impl Label {
  76. fn new(rng: &mut SmallRng) -> Self {
  77. Label([
  78. ADJECTIVES.choose(rng).unwrap(),
  79. COLOURS.choose(rng).unwrap(),
  80. NOUNS.choose(rng).unwrap(),
  81. ])
  82. }
  83. }
  84. static ADJECTIVES: &[&str] = &[
  85. "pretty",
  86. "large",
  87. "big",
  88. "small",
  89. "tall",
  90. "short",
  91. "long",
  92. "handsome",
  93. "plain",
  94. "quaint",
  95. "clean",
  96. "elegant",
  97. "easy",
  98. "angry",
  99. "crazy",
  100. "helpful",
  101. "mushy",
  102. "odd",
  103. "unsightly",
  104. "adorable",
  105. "important",
  106. "inexpensive",
  107. "cheap",
  108. "expensive",
  109. "fancy",
  110. ];
  111. static COLOURS: &[&str] = &[
  112. "red", "yellow", "blue", "green", "pink", "brown", "purple", "brown", "white", "black",
  113. "orange",
  114. ];
  115. static NOUNS: &[&str] = &[
  116. "table", "chair", "house", "bbq", "desk", "car", "pony", "cookie", "sandwich", "burger",
  117. "pizza", "mouse", "keyboard",
  118. ];