handlebars-lang / handlebars.js

Minimal templating on steroids.
http://handlebarsjs.com
MIT License
17.81k stars 2.04k forks source link

Partials are too slow #1991

Open mohd-akram opened 10 months ago

mohd-akram commented 10 months ago

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.

Extended benchmark with indenting