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

Improve performance by caching templates? #24

Open
mbostock opened this issue Jan 11, 2021 · 0 comments
Open

Improve performance by caching templates? #24

mbostock opened this issue Jan 11, 2021 · 0 comments
Labels
enhancement New feature or request

Comments

@mbostock
Copy link
Member

mbostock commented Jan 11, 2021

Modifying the renderHtml function (and likewise renderSvg) could greatly improve the performance of Hypertext Literal when rendering the same template over and over again. Something like:

const cache = new Map();

function renderHtml(string) {
  if (cache.has(string)) return cache.get(string).cloneNode(true);
  const template = document.createElement("template");
  template.innerHTML = string;
  const {content} = template;
  cache.set(string, content);
  return content;
}

But, open questions:

  1. When should the cache be cleared (e.g., after a requestAnimationFrame or Promise.resolve())? Should caching be optional or configurable? Should the size of the cache be? Should the cache be scoped to individual literals using a WeakMap?

  2. Hypertext Literal may currently inline interpolated values into the generated HTML to avoid needing to walk the tree and substitute dynamic values into placeholders (e.g., comments). However when caching, it might be desirable to always use placeholders, so that the generated template is never dependent on the interpolated values and can be shared.

I’d also love realistic benchmarks that we can use to measure these potential performance improvements.

@mbostock mbostock added the enhancement New feature or request label Feb 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant