jsframework.rs 3.3 KB

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