1

I couldn't find another question similar to this except this one: Storing Data in a Variable vs Inline Arithmetic

However that question was specific to Java, hence the micro memory management is really left to the JVM. My question relates to server-side JavaScript (ala NodeJs), and certain front-end frameworks like Ionic Framework.

Suppose we have some data and a function to utilize them.

var x;
var y;
var z;

var postRequest = function (x, y, z) {
    // some logic.
}

Data x, y, z need to be parsed and audited before passing into postRequest.

Is there any performance-memory-related advantage in just parsing the data inline as the parameter? As such (pay no mind to the arithmetic) :

postRequest( (x % y) / (z * (y % z)) );

vs this

var x1 = (x % y);
var y1 = (y % z);
var y2 = z * y1;
var x2 = x1 / y2;
postRequest(x2);

The latter is definitely easier to maintain and clean up later if needed. But do we sacrifice anything memory wise? I'm talking on a mass server scale, in a hypothetical situation where the function is run in the requests of thousands of users. Can we save anything performance wise running inline rather than creating references?

4
  • It's not a performance issue you'll see, but if you pass in those variables as a formula, you do not have the 3 chunks of memory stored for caching those values. You simply have the calcuated function parameter, which is used for whatever. So it's minuscule. Commented Sep 1, 2016 at 18:26
  • @SterlingArcher: If the function is compiled optimised, the 4 variables will probably never meet memory at all but stay in registers, just like in the inline version.
    – Bergi
    Commented Sep 1, 2016 at 19:44
  • @Bergi what do you mean stay in registers? Doesn't it take time for the system to create memory for the 4 variables versus the 1 parameter? Commented Sep 1, 2016 at 20:07
  • @SterlingArcher I'm saying the compiler can completely optimise away any variables, no memory access should be needed. Like the linked Java answer says: Let's leave the microoptimisations to the JIT.
    – Bergi
    Commented Sep 1, 2016 at 20:16

0

Your Answer

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