Skip to content

minifier: Simplify variable reassignment by using input parameter directly #15908

@camc314

Description

@camc314

The current implementation of foo unnecessarily reassigns the input parameter to a new variable (node = inputNode) solely to change its type. This can be simplified by using the parameter directly and removing the redundant variable declaration.

Current Code

// input
export function foo(inputNode) {
  // re-assign to change type
  let node = inputNode;

  do {
    node = node.parent
  } while (node !== null)

  return node
}

Proposed Optimization

The reassignment is unnecessary. We can rewrite the function using the parameter node directly and simplify the loop syntax:

// expected output
export function foo(node) {
  do
    node = node.parent
  while (node !== null)
  
  return node
}

This allows users to change the type of node without encountering the cost of declaring an additional variable.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions