1

I have multiple helper methods and I need multiple arrays as parameters. My question is if is more efficient to get them in each method:

component.get("v.someArray");

or call the function with this variable as parameter?

this.someMethod(component, someArray);

I can probably measure that, but I'm asking if somebody did that. Thank you

More details: Is it more efficient to do:

calculateSomething: function (component){
    var somevariable = component.get("v.myArray");
    ...

or

calculateSomething: function (component, myArray){
    ...
  • can you add some more details – Himanshu Nov 15 '17 at 9:24
3

tl;dr Do whichever is easier to maintain in your code base.

I did some testing, and given an array of 10,000,000 items, with 1,000 functions called, there was approximately a 0.5 to 5 millisecond delay each time get was called. This is actually an incredibly small amount of time compared to the overall transaction. In the "call get once and pass in the value" model, 1,000 function calls took 0.5 to 5 milliseconds. In the "call helper X times, which calls get" model, 1,000 function calls took 118 to 206 milliseconds.

So, there's obviously a non-zero cost to calling get, but it doesn't really matter in most cases. Anything that the function happens to be doing is going to use a lot more processor time than concerning yourself with how often get is called, unless you're very specifically doing something incredibly expensive like rendering an image or something where you're trying to call get for each pixel of data.

Instead, consider the maintenance aspect. If you have a helper method called from 1,000 different controller methods, and it expects a value instead of a component, and you decide to change the attribute name, you now have to fix 1,000 lines of code instead of just 1.

Conversely, if you have one method that calls 1,000 helper methods that accept a component, and you change the attribute name, then you still have to fix 1,000 lines of code instead of just 1.

Choose the method that you believe will be the least amount of maintenance in the future. If you have a few helpers called from many locations, you probably want to put the get calls in the helper methods. If you have many helpers called from a few locations, then you might consider having the callee call get as a matter of performance. Just make sure you document the reason for the method you used if you commingle the two techniques.

If you have any doubt at all, put the get calls in the helper methods. Odds are, this pattern will help you more often than it will hinder you. I've used this pattern in basically every single component I've ever written (except for any components small enough that no helper was written, such as experimental/test components), and it's never been a problem with performance, and maintenance is easy enough. Consistency in your code base is often more important than performance alone, so I would choose one specific pattern and use it exclusively unless you absolutely must optimize based on calling patterns.

|improve this answer|||||
  • ok, thank you, that makes sense. Honestly, I'm quite surprised, because I was expecting that get calls will take much more time. But anyways, interesting results. – Karel L Nov 15 '17 at 11:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.