Skip to content

Commit 51808b9

Browse files
committed
add nested partials benchmark
1 parent 080b5e0 commit 51808b9

File tree

1 file changed

+57
-2
lines changed

1 file changed

+57
-2
lines changed

benches/bench.rs

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ extern crate serde_derive;
55

66
use criterion::Criterion;
77
use handlebars::{to_json, Context, Handlebars, Template};
8+
use serde_json::json;
89
use serde_json::value::Value as Json;
910
use std::collections::BTreeMap;
1011

@@ -211,12 +212,65 @@ fn large_nested_loop(c: &mut Criterion) {
211212
});
212213
}
213214

215+
fn deeply_nested_partial(c: &mut Criterion) {
216+
use std::iter::repeat;
217+
let mut handlebars = Handlebars::new();
218+
219+
handlebars
220+
.register_partial(
221+
"nested_partial",
222+
r#"{{#each baz}}
223+
<div class="nested">
224+
{{this}}{{#if (not @last)}}++{{/if}}
225+
</div>
226+
{{/each}}"#,
227+
)
228+
.expect("Invalid template format");
229+
230+
handlebars
231+
.register_partial(
232+
"partial",
233+
r#"
234+
<div class="partial">
235+
{{#each bar}}
236+
{{>nested_partial}}
237+
{{/each}}
238+
</div>"#,
239+
)
240+
.expect("Invalid template format");
241+
242+
handlebars
243+
.register_template_string(
244+
"test",
245+
r#"
246+
<div class="test">
247+
{{#each foo}}
248+
{{>partial}}
249+
{{/each}}
250+
</div>"#,
251+
)
252+
.expect("Invalid template format");
253+
254+
let data = json!({
255+
"foo": repeat(json!({
256+
"bar": repeat(json!({
257+
"baz": repeat("xyz").take(7).collect::<Vec<_>>()
258+
})).take(7).collect::<Vec<_>>()
259+
})).take(7).collect::<Vec<_>>()
260+
});
261+
262+
let ctx = Context::wraps(data).unwrap();
263+
c.bench_function("deeply_nested_partial", move |b| {
264+
b.iter(|| handlebars.render_with_context("test", &ctx).ok().unwrap());
265+
});
266+
}
267+
214268
#[cfg(unix)]
215269
criterion_group!(
216270
name = benches;
217271
config = profiled();
218272
targets = parse_template, render_template, large_loop_helper, large_loop_helper_with_context_creation,
219-
large_nested_loop
273+
large_nested_loop, deeply_nested_partial
220274
);
221275

222276
#[cfg(not(unix))]
@@ -226,7 +280,8 @@ criterion_group!(
226280
render_template,
227281
large_loop_helper,
228282
large_loop_helper_with_context_creation,
229-
large_nested_loop
283+
large_nested_loop,
284+
deeply_nested_partial
230285
);
231286

232287
criterion_main!(benches);

0 commit comments

Comments
 (0)