This is a general question. Using an example below, suppose that I have a Javascript that performs an AJAX call on a PHP script to pull data from a database. I can choose to:
a) manipulate the data on the PHP script itself before encoding an sending it back to the Javascript, OR;
<?php
while ($row = mysqli_fetch_assoc($result))
{
extract($row);
array_push($data, array('product' => $product, 'discount' => $price*0.15));
}
echo json_encode($data);
?>
b) get raw data from the PHP script and do the manipulation on the Javascript.
<SCRIPT type='text/javascript'>
$.each(json, function(index, element) {
element.discount = 0.15*element.price
}
</SCRIPT>
Though I already know that I can get better performance (server-side) using option b), I would like to hear from the community whether it is a wiser choice and whether there are any logical arguments against it which I may have overlooked. Thanks!
NOTE:
- This is just a simple scenario, the calculations are more complicated than *0.15.
- Javascript is a must-have to pull data and for application to work.