On my page is a sample of the database:

<?php $row = mysql_fetch_array(mysql_query('SELECT * FROM `myTable`); ?> 

Which option is better (faster and slower), if there are more than 1000 such samples?

1 option:

 <span id="textA"><?php echo $row['A']; ?></span> <span id="textAA"><?php echo $row['A']; ?></span> <span id="textAAA"><?php echo $row['A']; ?></span> 

Option 2:

 <span id="textA"><?php echo $row['A']; ?></span> <script> document.getElementById("textAA").textContent = document.getElementById("textA").textContent; document.getElementById("textAAA").textContent = document.getElementById("textA").textContent; 

... and so on.

  • It would take a long time to explain this. There's a whole system there.) - nick

4 answers 4

I will not ask why you need it, but the first option is undoubtedly better. Because if you do this via php, then for the server, no matter how many variables are output, and the page generation time will increase by such a unit of time, which can be neglected even in highly accurate calculations.

The second option will slow down all the scripts for the user, and may just hang the page.

  • Do you clearly understand that the article clearly states that .textContent better than .innerText ? - Regent

Both options are bad for how the code is written. It is not clear why the loop is not used, and why in the case of JS, each time you extract a value from a DOM element, instead of storing it in a variable.

And as a matter of fact - in practice data substitution into a template both on the server and in the browser is used, everything depends on the organization of the web application.

    If there are a lot of copies and they are large, then it is better not to drive the extra volume over the network, but use the client script. And put the string in the variable and add new elements, and not push the elements in advance in the markup.

    In any case, the execution of the script on the client should reduce the load on the server, but I do not think that this is essential for you.

    Well, of course, if js is disabled in the browser, adding elements will not work. If elements are needed even with js disabled, then you definitely need to add them on the server.

      Option 1 will be faster, and cycles will make the system "inhibited".

      • "In any case, the execution of the script on the client should reduce the load on the server, but I do not think that this is essential for you." And the question is why do you need js? The script is not big, do it through php + can really disable js, and php can not be disabled :) - Denis Kotlyarov