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

Performance & clarity concerns with context object #83

Open
curran opened this issue Mar 27, 2017 · 0 comments
Open

Performance & clarity concerns with context object #83

curran opened this issue Mar 27, 2017 · 0 comments

Comments

@curran
Copy link
Owner

curran commented Mar 27, 2017

One thing that doesn't quite feel right is how so many new objects are created during the render pass when using a context object:

// Computes the data to pass into the data join from component invocation arguments.
function dataArray(data, context) {
  data = Array.isArray(data) ? data : [data];
  return context ? data.map(d => Object.assign(Object.create(context), d)) : data;
}

What if instead of creating new objects and shallow merging each datum with the context object, we split up the API such that the lifecycle hooks refer to the context object explicitly, rather than having stuff from the context object implicitly end up on d?

Before:

const myComponent = d3.component("div")
  .render((selection, d) => {
    selection
        .text("d")
        .on("click", d.onClick);
  });

After:

const myComponent = d3.component("div")
  .render((selection, d, context) => {
    selection
        .text("d")
        .on("click", context.onClick);
  });

Pros:

  • Clear separation of "data" and programmatic elements like callbacks (which would go on the context).
  • The data array is unmodified, no surprises when inspecting the datum objects.

Cons:

  • Custom logic will be required to specify a data property for all data elements.
  • More burden on developers to think about what goes where.
@curran curran changed the title Performance concerns with context object Performance & clarity concerns with context object Mar 27, 2017
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

No branches or pull requests

1 participant