jsframework.rs 3.3 KB

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