Skip to content

Ensure that live-binding helpers don't re-execute when used in CommonJS #8087

@chocolateboy

Description

@chocolateboy

Feature Request

Several helper functions redefine themselves (i.e. replace themselves with a more optimized version) at runtime e.g.:

  • construct
  • extends
  • get
  • getPrototypeOf
  • set
  • setPrototypeOf
  • typeof
  • wrapNativeSuper

This has the advantage of scoping the optimization to the helper, which reportedly makes inline helpers more amenable to minification/tree-shaking in some cases.

The downside is that it a) makes the code less clear, which makes it harder to spot bugs, and b) doesn't work with (and slows down) the (default, non-ES6) external helpers.

If minification/tree-shaking is a concern, external helpers should be used, rather than making the code slower for external helpers and less maintainable for everyone.

I propose that these optimizations should be implemented without redefining the helper e.g.:

Before

export default function _setPrototypeOf (o, p) {
    _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf (o, p) {
        o.__proto__ = p
        return o
    }

    return _setPrototypeOf(o, p)
}

After

function _objectSetPrototypeOf (o, p) {
    o.__proto__ = p
    return o
}

var __setPrototypeOf = Object.setPrototypeOf || _objectSetPrototypeOf

export default function _setPrototypeOf (o, p) {
    return __setPrototypeOf(o, p)
}

Which, in this case, can be reduced to:

function _objectSetPrototypeOf (o, p) {
    o.__proto__ = p
    return o
}

export default Object.setPrototypeOf || _objectSetPrototypeOf

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions