Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Partials are too slow #1991

Open
mohd-akram opened this issue Sep 2, 2023 · 0 comments
Open

Partials are too slow #1991

mohd-akram opened this issue Sep 2, 2023 · 0 comments

Comments

@mohd-akram
Copy link
Contributor

mohd-akram commented Sep 2, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants