DEV Community

Manoj
Manoj

Posted on

1

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)

SurveyJS custom survey software

JavaScript Form Builder UI Component

Generate dynamic JSON-driven forms directly in your JavaScript app (Angular, React, Vue.js, jQuery) with a fully customizable drag-and-drop form builder. Easily integrate with any backend system and retain full ownership over your data, with no user or form submission limits.

Learn more