0

Suppose I have the following classes Test1 and Test2. Test1 declares two functions, fA and fB, and fA is called inside fB. Test2 declares fA inside fB. Would either of them have a performance advantage over the other? Assume that fA is recursive and will be called once every time fB is called.

Example:

function Test1() {
    this.fA = function() {
        //function body here...
    };

    this.fB = function() {
        //some code...

        this.fA();
    };
};

function Test2() {
    this.fB = function() {
        let fA = function() {
            //function body here...
        };

        //some code...

        fA();
    };
};
5
  • 3
    Unless the functions are going to be called millions and millions of times the difference is completely ignorable, if there's a difference at all. Commented Oct 1, 2019 at 15:44
  • 1
    There shouldn't be a notable performance differences if the function definitions are the same. Commented Oct 1, 2019 at 15:45
  • 1
    Obligatory link to Eric Lippert on performance Commented Oct 1, 2019 at 15:49
  • 1
    Note: the example provided is a typical use case for a prototype Commented Oct 1, 2019 at 15:52
  • There are reasons why you would do one over the other. Commented Oct 1, 2019 at 16:00

1 Answer 1

1

In the first case, fA gets created once; in the second, it gets created each time fB is called. So the second is surely slower, but as @Pointy points out, probably not enough to worry about.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.