0

i wanted to ask this for a while. what is the difference in terms of efficiency for using javascript or PHP in performing a regular expression search and replace functionality?

if i were to develop a website, both language can perform the same function but im just curious what normally professional web developers use.

i want to know the standards when to use javascript or PHP if both can perform the same.

Javascript var result = str.replace();

PHP preg_replace();

bobbyjones
  • 2,029
  • 9
  • 28
  • 47

3 Answers3

5

It really isn't a matter of performance in this case, but rather, where you want to do the replacing. If you want to do something in the client-side, you use JavaScript. If you want to do it in the server-side, you use PHP.

Also: If you can get to choose (say, instead of doing the replace in PHP and then echoing it, just replacing in the client-side with JavaScript), I'd use JavaScript because the users' browsers do the job instead of your server, and therefore it would reduce the server load. But this would only matter if you had thousands of replaces per minute or something like that.

federico-t
  • 12,014
  • 19
  • 67
  • 111
  • I really don't know which one is faster, it's very hard to tell. But for what OP may care, doing it in JavaScript would be better _if_ he's trying to reduce server load (as it would delegate the replace job to the user navigator). – federico-t Oct 24 '13 at 03:25
1

More than likely the answer is going to depend on more than the raw performance of it. For example, let us assume that if you are using PHP in combination with Javascript, you likely mean to say that Javascript is happening on the browser side, and PHP on the server side.

If you need to validate the format of a phone number for example, you could do that both before the form is submitted (in Javascript inside the browser) or server side in PHP.

Let's say you did it in Javascript, now what happens when a user without Javascript comes by and fills out your form? Or more likely, a spam bot? Incorrectly formatted data is now being passed back to your server and might trigger errors or other unexpected conditions. In this example you will likely want to validate the phone number on the server side at the very least. But you might want to be able to tell the user that the phone number is invalid before submitting without having to wait for the server to validate it and send back errors, so doing it in Javascript on the browser side in addition to PHP on the server side might be the way you choose to implement it.

I hope this helps to demonstrate some of the considerations of where to implement your logic.

Chris Cherry
  • 28,118
  • 6
  • 68
  • 71
0

A very unreliable bench mark shows that the browser may actually be faster. Although there may be some very large optimizations going on in the browser using the test case I made versus the PHP case I made.

This post also supports the findings: Speed of PHP vs JavaScript?

Running this code in PHP:

<?php
    require("Logger.php");

    $string = "apples and bananas";

    $logger = new Logger("Start");
    for ($x = 0; $x < 1000000; $x++) {
        $newString = str_replace("bananas", "oranges", $string);
    }
    $logger->log("Finish");

    echo $logger->status();

Came back with 10867ms on my local machine (WAMP). (And removing the class I'm using for logging does NOT speed it up.)

Running the following JS code (http://jsfiddle.net/MQKLT/3/) is returning roughly 343ms (and is visibly faster).

function go() {
    var str = "apples and bananas";

    var newString = "";

    var d = new Date();
    var n = (d.getSeconds() * 1000) + d.getMilliseconds();

    for (var x = 0; x < 1000000; x++) {
        newString = str.replace("bananas", "oranges");
        newString = newString + x;
    }

    var d2 = new Date();
    var n2 = (d2 .getSeconds() * 1000) + d2.getMilliseconds();

        //console.log("First: " + n + " Second: " + n2 + " Total: " + (n2 - n));
        alert("First: " + n + " Second: " + n2 + " Total: " + (n2 - n) + " Output: " + newString);

}

As I said, the browser may be doing some major optimizations. The value I posted above was tested in Chrome. I do get similar times in IE and a faster time in Firefox (which suggests to me that optimizations are happening.)

With that in mind: I'm a lazy programmer and I tend to go with what is most convenient for me as long as it doesn't compromise security. (IE, string manipulation in regards to validation should be done in PHP / away from client control) It makes absolutely no sense to do an AJAX request for a string replace by itself. For real world cases it depends on a lot of variables such as the browser involved, the PC involved, the server involved.

If you are worried about string replace performance, there is likely something wrong with the logic you are using to perform your task.

Community
  • 1
  • 1
teynon
  • 7,540
  • 10
  • 63
  • 106