Preact: Potential Optimization: Bail when no props

Created on 7 Nov 2019  路  4Comments  路  Source: preactjs/preact

Note: this likely represents a breaking change for users who may rely on periodic redraws to consume external state. Seems like a bad use case, but one that undoubtedly exists.

The memo HOC is a wonderful optimization. However, it seems there are use cases where it shouldn't be necessary. For example, if a component accepts no props, it should never redraw as a result of its parent redrawing.

I can provide more context about my situation if desired, but I'm curious if this is possible/reasonable. If it sounds like something worth adding, I'd be thrilled to write the PR.

Thank you for your feedback!

Most helpful comment

@jpodwys Your logic makes sense, but how would Preact infer that a component doesn't accept any props? Also it's hard to do these optimizations without a signal like memo(), since a Component could accept no props but be dynamic:

const Impure = () => <div>{Date.now())</div>

All 4 comments

@jpodwys Your logic makes sense, but how would Preact infer that a component doesn't accept any props? Also it's hard to do these optimizations without a signal like memo(), since a Component could accept no props but be dynamic:

const Impure = () => <div>{Date.now())</div>

Thank you for making time for me!

You're absolutely right. The Impure component is just the kind of thing I was referring to when I mentioned use-cases that rely on periodic redraws. It's not something people should rely on IMO, but it certainly happens.

Perhaps detecting props usage isn't possible in class components, but is there a chance it's possible in function components? I'm making some assumptions here, but if a function component's vnode is indeed a function (or contains a function), is it possible to check f.length to determine whether arguments are present? Or would that array always have a length greater than zero due to internal plumbing?

Certain babel plugin add props at creation time that are later deleted for example __source which would cause a discrepancy between production and development which is not ideal.

Another thing is that if we would check props on every component that this introduces another form of overhead linear to wrapping every component in memo or having every component execute shouldComponentUpdate most components that accept no props are most of the time so cheap that this can be neglected and most commonly are too small of a fraction of the trickled down update to cut down upon.

As I'm thinking it over more, I'm realizing there are legitimate use-cases involving prop-less components that ought to redraw when their parents redraw.

Thanks again for fitting me into your very busy schedule, my friends!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

marcosguti picture marcosguti  路  3Comments

adriaanwm picture adriaanwm  路  3Comments

philipwalton picture philipwalton  路  3Comments

jasongerbes picture jasongerbes  路  3Comments

paulkatich picture paulkatich  路  3Comments