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

More Performant Templates Clonning #3

Merged
merged 6 commits into from
May 22, 2022
Merged

More Performant Templates Clonning #3

merged 6 commits into from
May 22, 2022

Conversation

yhdgms1
Copy link
Owner

@yhdgms1 yhdgms1 commented May 21, 2022

As Ryan Carniato said here, nodes should be cloned at the first place, because this is more performant.

Unfortunately, this doesn't always work with Grim, because the values are inserted into the template, and the output looks like this:

let name = getName();
let el = template(`<div>Hello, ${name}!</div>`)

Therefore, only static solutions will be put at the beginning, and later will be copied via cloneNode.
This is how it looks in practice.

Look at the code

Input

function Greet(name) {
  const el = (
    <div>
      <span class={styles.greeting}>{name}</span>
    </div>
  );

  const emoji = (
    <span>💀</span>
  );

  el.appendChild(emoji);

  return el;
}

Before

import { template as _tmpl } from "grim-jsx/dist/runtime.js";

function Greet(name) {
  const el = _tmpl(`<div><span class="${styles.greeting}">${name}</span></div>`);

  const emoji = _tmpl(`<span>💀</span>`);

  el.appendChild(emoji);
  return el;
}

After

import { template as _template } from "grim-jsx/dist/runtime.js";

let _tmpl = _template(`<span>💀</span>`);

function Greet(name) {
  const el = _template(`<div><span class="${styles.greeting}">${name}</span></div>`);

  const emoji = _tmpl.cloneNode(true);

  el.appendChild(emoji);
  return el;
}

@yhdgms1 yhdgms1 merged commit 8283d00 into master May 22, 2022
@yhdgms1 yhdgms1 deleted the test-clone-node branch May 22, 2022 05:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant