Skip to content

Tree shaking

Leaving unneeded parts of a set of modules out of the output.

Software Development

What is tree shaking?

Tree shaking removes unneeded code parts from the output. In JavaScript, the statically analysable structure of ES modules with imports and exports helps this process. What can be removed depends on the tool, module structure, configuration and side effects. The term does not promise that every unused piece of code can be safely identified.

In a fictional portal, only date formatting is needed from a utility library. The team checks which other parts disappear from the generated package. A blanket declaration of no side effects would be risky if loading a module registers required functions or includes styles. Those effects must remain. Size and behaviour are compared in the actual result; enabling an optimisation option proves neither savings nor correct function.

Sources

Articles on this topic

Software Development with ASES

Custom software, web and mobile apps