This question already has an answer here:
This is not an bug or issue. But while I was reading about a lot of performance realated articles I came to know about how javascript variables take more resources to load and so it is better to put the global variable for javascript into a local variable to work - specially in for loops.
So I was wondering if such thing also happens on $_POST with PHP. As in will it give me a performance improvement if I have lot of post data. Save that in a local array say $post_data = $_POST;
And then I reference in where required.
</div>
Not really worth the time. $_POST
is already an array in memory, so no need to duplicate it.
To get the size of $_POST
in bytes, you can do this:
strlen(serialize($_POST));
(serialize
will turn any object into a string which can be saved and unserialize
d. )
FWIW in for
loops, if you don't cache the length of the array and instead use something like this...
for(var i = 0; i < myarray.length; i++) {}
...JS will have to recalculate .length
each time it starts an iteration, so it's expensive, particularly over big arrays.
In php the main concern with globals is memory. Performance difference in accessing a global or local scoped variable is negligable.
Not sure about the JavaScript part (can you provide a reference, please?). In PHP this would effectively create a copy of $_POST array and hence would use more memory without having any effect on speed improvement.
If you make:
$post_data = $_POST;
PHP will create just reference to info in $_POST
variable, so you will not have more memory usage, till changind info in $post_data
. If you try to change data when PHP will create copy of information in memory.