Skip to content

Change the minified version of undefined to 1..u #591

Closed

Description

@j-f1

This returns undefined, and it’s two bytes shorter than void 0 (it’s also 5 bytes shorter than undefined). The . operator has a higher priority than the void operator, so it’s safe. A plugin could also replace all instances of void 0 in other code with 1..u.

Activity

  1. loganfsmyth commented on Jun 20, 2017

    @loganfsmyth
    Member

    Doesn't seem like a good idea since everything would break if someone did Object.prototype.u = 4. We could hoist the definition with a var u = void 0; at the top of the file if there are more than a couple references to undefined though.

  2. j-f1 commented on Jun 20, 2017

    @j-f1
    ContributorAuthor

    @loganfsmyth If you think it’s likely that someone is going to do that, then it could be changed to any other valid character. 1..µ only takes up one more byte in UTF-8.

  3. loganfsmyth commented on Jun 20, 2017

    @loganfsmyth
    Member

    Right, but it still means your exposing your code to the potential for deliberate breakage that wouldn't be present otherwise.

  4. ashsearle commented on Jul 5, 2017

    @ashsearle
    Contributor

    A little brain-dump for @loganfsmyth's hoisting suggestion...

    Remember that undefined is the default value for a variable, so this could be shorter at var u; or let u; (although I guess it would be more robust to use const u = void 0)

    Also, there may be opportunities to piggy-back on a list of other variables declared, so could be as little as 2 additional characters. e.g. if a and b are already defined in a var a,b; declaration, then we only add ,u to get var a,b,u;

    Or, if you want to use const for robustness, you place the const immediately after an uninitialised variable declaration: var a,b;const u=a;

  5. loganfsmyth commented on Jul 5, 2017

    @loganfsmyth
    Member

    Hah, good call about not needing =void 0; :P

    One other thing I'll add, inserting a new variable is potentially iffy, we'll need to know whether the file being minified is a <script> or not, since that would potentially be creating a new global variable.

  6. boopathi commented on Aug 22, 2017

    @boopathi
    Member

    This is already brought up in #25 (comment) (last point).

    Agree that adding new variables to toplevel isn't safe enough. But this requirement is something that has come up earlier - in minify-builtins, we solve it by inserting this new variable in certain parent scopes (function/block && NOT program) based on number of usages. The same thing can be done for void 0.

    For example,

    function foo(x, y, z) {
      this.x = x === void 0 ? "x" : x;
      this.y = y === void 0 ? "y" : y;
      this.z = z === void 0 ? "z" : z;
    }
    function bar(x, y, z) {
      this.x = x === void 0 ? "x" : x;
      this.y = y === void 0 ? "y" : y;
      this.z = z === void 0 ? "z" : z;
      this.k = 2;
    }

    can be transformed to

    function foo(x, y, z) {
      let _void0;
      // ...
    }
    function bar(x, y, z) {
      let _void0;
      // ...
    }

    instead of hoisting _void0 to program.

    I'll close this as duplicate of #25 as it's easy to refer to that issue for new features as it is in a single place. Feel free to discuss here or comment to reopen if necessary.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions