0

My question has to do with optimizing my javascript code to be more efficient.

I have a function that is called frequently but all the code inside is only needed to be called once, like so:

function removeBlankCanvas() {
    if ( --numberOfChartsStillLoading == 0 ) {
        //do stuff
    }
}

This works fine but the function is still called countless times after numberOfChartsStillLoading is 0, and I know it will never be 0 again. So I'm thinking about doing something like this:

function removeBlankCanvas() {
    if ( --numberOfChartsStillLoading == 0 ) {
        //do stuff

        removeBlankCanvas = function() {
            return true;
        }
    }
}

Could this be more efficient code? For instance, if the function was called millions of times? I'm asking strictly for curiosity's sake.

2 Answers 2

2

Your performance bottleneck is very unlikely to be a simple if statement.

This trick could actually cause unexpected result, because functions are immutable, and you are very likely to have callbacks that will keep a reference to the original function, even after you changed the name reference to another function.

My 2 cents: find real performance issues to optimize.

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

Comments

0

I think you can create custom event and attached to the caller. You can remove the event from caller based on any condition you want.

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.