Question Overflow
Question Overflow

Reputation: 11255

Manipulating Data from an AJAX Call: PHP or Javascript?

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:

Upvotes: 0

Views: 563

Answers (3)

Pr Shadoko
Pr Shadoko

Reputation: 1698

Main difference between the 2 options is that with server time, each time you need to change anything in your data, you need to call the server which is badly impacting performances (from user point of view).

You can have a mixed approach: build your page with all pre-calculated data and have the same process implemented in JS used to update the formulas when But I would advise you to do so, as you double your development costs and moreover you will face exploding maintenance effort as it is very hard to keep both PHP & JS consistent.

2 general advices to make your choice:

  1. I think you shouldn't consider the performance point as your main concern but the functional point. Make the best product and then optimise it as there's no point in building something fast if not good.
  2. Always measure! Implement A & B and compare results. It takes some coding time, but it's the only way to really answer your question.In the case this point isn't important enough to spend time on AB testing it, then it is probably that both options are as good in your case

Upvotes: 0

Layke
Layke

Reputation: 53146

Some things you do not want to be stingy on when it comes to performance or time taken to execute.

  • Cryptography is one of them.
  • Anything with a financial aspect to it, is another.

The fact that we can do XYZ on the client layer, through JavaScript doesn't mean that we should be compelled to do it. What happens

A) If JavaScript is turned off. B) You rely on the value POSTed from JavaScript. (Imagine a shopping cart which has $_POST['total_amount'] which is used by the server. I am going to edit that POST or hidden input to get free things.

If what you are doing is purely informative, then this does not matter as much, but if you rely on any information, then you should be checking the prices multiple times.

Additionally, what happens when you want to start providing an API or something to your service. Your transport is now broken because it doesn't contain the calculated prices.

DO it server side, at whichever layer better. As Duncan says, SQL could be one option, or PHP. I don't have a preference on this one since any gains for a smallset of items will be largely negligible.

Upvotes: 2

duncan
duncan

Reputation: 31912

you'd be quickest doing this in your SQL before it even reaches your PHP. I'd also be surprised if the javascript performance is better than the PHP performance (how do you know this is better for you? Just curious...). Do as much as you can server-side is usually a good rule of thumb. You can always add more servers and so on, but there's not much you can do to control client-side performance.

Upvotes: 0

Related Questions