I usually put the result of a wix-data query into a var and then pass that whole array of objects on to other functions. I do this some 8 or 9 times (pass it on as an argument to a function).
Does anyone know which of the following methods are (a) more memory efficient and (b)faster:
- passing the array from function to function, as I do now?
- declaring it as a global var, assign the array to it once and not pass it on to other function, just access it directly?
I don’t know about speed but a best practice is to isolate function from its environment. It’s better for testing and debugging
Also it prevent one wrong manipulation to corrupt all function call (look out for pure function in javascript)
Did some more testing. It looks like arrays are being passed by reference. So if you do this:
- array1=query_collection();
- array2 = manipulate_queryresult(array1);
then array2 and array1 will be identical, holding the same changes.
So if this is true, passing arrays of objects does not occupy more memory, nor does make it slower, since there is no copying done.