Jamex
Jamex

Reputation: 1222

string vs array processing speed in javascript and php, and can an array be passed to php without manipulation?

a few questions on strings vs arrays for text processing.

Is it possible to pass a js array[] to php as an array without converting it to a string or using JSON? Just pass it as an ordinary array without manipulation.

In php and JS (or any other language), in general, which format is faster for processing or searching text (for very large arrays or text strings). Example:

string = abc,def,dfa,afds,xyz,afds,xxx

array = {"abc","xyz","xxx"}

Which is faster to use to search to see if xyz is present/match?

Which is faster to use to determine the index position of xyz?

Which has a smaller memory size/usage?

TIA.

Edit: the answer to point 1 is no I guess, but for point 2, please understand that I am asking because I am dealing with a program that makes concurrent ajax calls that requires the processing of very large arrays or text strings. The usability of the interface depends on the speed of the returned ajax calls. I had to scrap the original code because of this problem.

Upvotes: 2

Views: 3327

Answers (2)

deceze
deceze

Reputation: 522499

As for question 1, can you pass a native Javascript array to PHP, the answer is no.
Not only are Javascript arrays and PHP arrays incompatible (they're data structures of two different languages), the only communication between (client-side) Javascript and PHP is through HTTP, which only knows strings. Not numbers, not booleans, not objects, not arrays, only strings.

As for question 2, speed, it depends on many things, including your search algorithm and the string/array length. If your data structure is an array, use it as an array, not as a string. Readability and maintainability first, speed optimizations only when necessary. And you have to push the limits quite a bit more before you'll get into performance problems, your short examples are plenty fast enough either way.


Here's a test case I created that may answer your question: http://jsperf.com/string-search-speed
It really depends on your goal though. Searching within a string means you need to rule out the possibility of just matching a substring, for which you pretty much need a RegEx. Unless that's of no concern. On the other hand, stuffing everything into an object is orders of magnitude faster, but won't allow you to store the same string twice. Whether that's of concern or not I don't know. Be sure to run these tests on a wide variety of browsers, as the speed of individual tests varies greatly among Javascript engines.

And the more I play around with this the clearer it is that there's no answer. All tests score almost equally well in Chrome (safe for object lookup, which plays in a different league). Opera seems to have an enormously optimized str.search implementation which is on par with object lookups. In Safari all Regex tests are terribly slow but object lookups are the fastest of any browser. Firefox's str.indexOf is awesome, manual array looping not so much.

So again, there is no absolute answer (unless you use objects, which are always faster). Do what makes the most sense!

Upvotes: 3

dqhendricks
dqhendricks

Reputation: 19251

why do you say "without using JSON"? JSON is exactly what you are looking for. you turn the array into a JSON string, pass it to PHP, then have PHP parse the JSON back into an array object.

also, it sounds like you are doing premature optimization. most of the time, making your code easy to use is more important than shaving miliseconds off of your execution time. if you really want to know the speed of things, run some benchmarking tests.

Upvotes: 0

Related Questions