While investigating why rendering a few hundred objects was incredibly slow in Handlebars, I found that the root cause is partials (profiling showed most of the time spent in invokePartialWrapper, a lot of time is also spent in extend). See the following code:
import Handlebars from 'handlebars';
const count = 10000;
// Handlebars
Handlebars.registerPartial('partial', Handlebars.compile('1234'));
const template = Handlebars.compile('{{> partial}}');
// Native
function partial() { return '1234'; }
const template2 = () => `${partial()}`;
// Warm up
for (let i = 0; i < count; i++) {
template();
}
console.time('partial');
for (let i = 0; i < count; i++) {
template();
}
console.timeEnd('partial');
// Warm up
for (let i = 0; i < count; i++) {
template2();
}
console.time('native');
for (let i = 0; i < count; i++) {
template2();
}
console.timeEnd('native');
Output:
partial: 50.114ms
native: 0.409ms
Partials are 120x times slower than a regular function call.
While investigating why rendering a few hundred objects was incredibly slow in Handlebars, I found that the root cause is partials (profiling showed most of the time spent in
invokePartialWrapper
, a lot of time is also spent inextend
). See the following code:Output:
Partials are 120x times slower than a regular function call.
Extended benchmark with indenting