Skip to content

Commit

Permalink
add nested partials benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
cmrschwarz committed Jul 17, 2024
1 parent 080b5e0 commit 51808b9
Showing 1 changed file with 57 additions and 2 deletions.
59 changes: 57 additions & 2 deletions benches/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ extern crate serde_derive;

use criterion::Criterion;
use handlebars::{to_json, Context, Handlebars, Template};
use serde_json::json;
use serde_json::value::Value as Json;
use std::collections::BTreeMap;

Expand Down Expand Up @@ -211,12 +212,65 @@ fn large_nested_loop(c: &mut Criterion) {
});
}

fn deeply_nested_partial(c: &mut Criterion) {
use std::iter::repeat;
let mut handlebars = Handlebars::new();

handlebars
.register_partial(
"nested_partial",
r#"{{#each baz}}
<div class="nested">
{{this}}{{#if (not @last)}}++{{/if}}
</div>
{{/each}}"#,
)
.expect("Invalid template format");

handlebars
.register_partial(
"partial",
r#"
<div class="partial">
{{#each bar}}
{{>nested_partial}}
{{/each}}
</div>"#,
)
.expect("Invalid template format");

handlebars
.register_template_string(
"test",
r#"
<div class="test">
{{#each foo}}
{{>partial}}
{{/each}}
</div>"#,
)
.expect("Invalid template format");

let data = json!({
"foo": repeat(json!({
"bar": repeat(json!({
"baz": repeat("xyz").take(7).collect::<Vec<_>>()
})).take(7).collect::<Vec<_>>()
})).take(7).collect::<Vec<_>>()
});

let ctx = Context::wraps(data).unwrap();
c.bench_function("deeply_nested_partial", move |b| {
b.iter(|| handlebars.render_with_context("test", &ctx).ok().unwrap());
});
}

#[cfg(unix)]
criterion_group!(
name = benches;
config = profiled();
targets = parse_template, render_template, large_loop_helper, large_loop_helper_with_context_creation,
large_nested_loop
large_nested_loop, deeply_nested_partial
);

#[cfg(not(unix))]
Expand All @@ -226,7 +280,8 @@ criterion_group!(
render_template,
large_loop_helper,
large_loop_helper_with_context_creation,
large_nested_loop
large_nested_loop,
deeply_nested_partial
);

criterion_main!(benches);

0 comments on commit 51808b9

Please sign in to comment.