0

I'm trying to figure out what is the best efficiency wise argument structure to pass to a recursive function.

lets say I have a simple and a useless recursive function in JavaScript, and a variable of type string that holds, I don't know, like 10 000 000 characters

let str;
function test(arg,i){
   if(i<1000){
      test(arg,i+1);
   }
}

now I of course want to use this function and I want to pass the str variable as an argument.
I thought of two possible ways to pass it:

  1. simply passing it to the function call like this test(str,0).
  2. passing it inside an array or an object like this test([str],0) or test({string:str}).

I chose to go with option 2, why? well:
option 1 would create an argument variable and copy the content of str on each call of test() , that's why passing a string to a function is called a "pass by value" .
option 2 would pass a reference of [str] or {string:str} to each call of test() which means there would be no copying, so logically, based on my understanding, option 1 should have str.length*n or str.length*(n-1) (where n is how many times the function was called) more operations, which means option 1 is a lot less efficient than option 2.

my results : after testing the efficiency of the same example (with more calls) I didn't get any performance difference.
is that because my understanding is wrong?
is it because the function is not using arg in this example?
is it because the function is simple?

so if anyone that knows how JavaScript or in general programming languages work, or someone that have found a solution for this problem, I would be grateful for your explanation.

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
  • I'm no performance-expert either, but here are some thoughts: Using reference vs variable seems more like a memory-aspect than a perforance-aspect (of course memory will impact performance eventually). You're passing the value, but in this case it is not being stored in each function, so there's little impact. Your recursion is only called 1000 times, with may be too small too measure any difference. Your example doesn't return anything: could you update your example with the code you used for testing? – Kevin Driessen Aug 29 '21 at 09:01
  • @KevinDriessen the fact that you can change and use the argument being passed in one function call and it not changing its value in the other calls is a pretty well indicator that it is being coppied and when you copy things it will be less performant than another solution where you dont have to copy things. – fodil yacine Aug 29 '21 at 09:20
  • for the code it is the exact code i used in the example while doing 10 000 000 calls – fodil yacine Aug 29 '21 at 09:23
  • 1
    in JavaScript: `string` and `object` values are passed by-reference: **they are not copied or passed-by-value** - so passing a "large" object is just as expensive as passing an integer value. (The pedants will say JS isn't "pass-by-reference" but actually "reference-passed-by-value" as JavaScript doesn't support true `ref`-parameters - unless you count closures). Additionally, `string` values are immutable: you can never modify a string, instead all string operations (like `substr`, `toUpper`, etc) all return separate new `string` instances. – Dai Aug 29 '21 at 16:20
  • As @Dai just said. If you've noticed you can modify the string in one function and it doesn't affect the other, that's not because the string's value was copied to the new function, but rather because strings are immutable. If a function "changes" a string's value, that's actually a new string and doesn't affect the memory containing the original string. – 404 Aug 29 '21 at 16:22
  • Does this answer your question? [Is JavaScript a pass-by-reference or pass-by-value language?](https://stackoverflow.com/questions/518000/is-javascript-a-pass-by-reference-or-pass-by-value-language) – Dai Aug 29 '21 at 16:24
  • @Dai ah now i understand it better. – fodil yacine Aug 30 '21 at 07:57

0 Answers0