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
andwithArrowDefinition
grows linearly alongwith number of functions defined in the file (and both are similar)
Top comments (0)