0

My application is a local server that receive about 2/3 requests per seconds.

At each request, it stores and update data, process some calculation, update view (react), ...

I would like to know what is faster, when i have to use closures :

  • Simply create the function where I need it:

    var parentValue = 'ok';

    randomAsyncFunction(function() { console.log(parentValue); }

  • Create a "global" function and then bind the callback with needed values:

    function testCallback(value) { console.log(value); }

    var parentValue = 'ok'; randomAsyncFunction(testCallback.bind(undefined, parentValue));

Note: theses pseudo-codes will be executed 2/3 times per seconds. For the second example, the testCallback function will be created once, and the bind will be called instead of re-creating the function.

So, is it better or worse to use the second example ?

1
  • 23 requests per second? That's nothing. You should not need to care about performance. Commented Apr 27, 2016 at 11:30

1 Answer 1

1

Both bind and the closure function expression do create a new function object. Their difference in performance will be negligible. If you really care enough, run a benchmark with your actual code and real data to see which solution is faster.

In your case, you should only care which solution is more readable and maintainable. None is strictly better or worse than the other, you have to decide yourself which one you like better.

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.