Skip to content

Move variable declarations that are top level in function scope, to the function parameters. #1209

Open

Description

@lillallol

Here is a real world example, that has been formatted for readability purposes, of what terser gives me in one of my libraries:

export var n = (e) => {
		throw Error(e);
	},
	m = new WeakMap(),
	F = (e) => {
		let t = m.get(e);
		return t || n(0), t;
	};

This can be further minified like this:

export var n = (e) => {
		throw Error(e);
	},
	m = new WeakMap(),
	F = (e,t = m.get(e)) => t || n(0), t;

let and return and {,} are stripped. This is 14 characters in total, and given that we need to add a new , in the function parameters, we end up using 13 characters less.

This should be done only for functions that belong to the private api. (The example I provided actually belongs to the private api of my library. The export is used just for terser to not dead code eliminate the whole code)

Edit : Care should be taken for this to not be applied to function that gather the arguments with the triple dot operator.

Activity

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions