DEV Community

Manoj
Manoj

Posted on

function vs expression vs arrow definitions - Performance comparison (Part 3)

In this post we are going to compare performance of javascript functions created with function definitions vs function expressions vs arrow functions.

To test this, we created three types of files:

  • withFunctionDefinition.js - contains simple function definitions
  • withFunctionExpression.js - contains function definitions using function expression syntax
  • withArrowDefinition.js - contains function definitions with arrow syntax

withFunctionDefinition

  • Lets create a file with name withFunctionDefinition.js and add 1 million function definitions to the file.

withFunctionExpression

  • Lets create another file with name withFunctionExpression.js and add 1 million function definitions using expressions to the file.

withArrowDefinition

  • Lets create yet another file with name withArrowDefinition.js and add 1 million arrow functions.

Results

  • File withFunctionDefinition takes a constant time to get executed and has the best overall performance.
  • The time complexity of files withFunctionExpression and withArrowDefinition grows linearly alongwith number of functions defined in the file (and both are similar)

References

https://github.com/manojadams/js-objects-memory/blob/main/function-definition_vs_function-expression_vs_arrow-function/README.md

Top comments (0)