Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question
For question 1: stackoverflow.com/questions/7016701/… – NullUserException Sep 8 '11 at 23:11

2 Answers

up vote 2 down vote accepted

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!

share|improve this answer
Thanks for the explanation of question 1. For question 2, let's just say that I have an array that is 10,000 long and a text string that contains the same elements separated by commas. Which is faster to use to see whether one of the element is "xyz"? – Jamex Sep 8 '11 at 23:38
@Jamex Why not test it? – deceze Sep 9 '11 at 0:17
@Jamex - there are many variables in a "how fast is..." questions, such as whether Array's indexOf or each methods are avaialble, general string or array processing speed, and so on. These tend to vary (sometimes hugely) between browsers, so whatever is faster in one browser likely isn't in another. For large arrays, an index really helps, as does ordering. As others have said, just do what seems sensible, easily maintainable and doesn't waste development time. Then if you have performance problems, spend your effort finding and addressing the specific issues. – RobG Sep 9 '11 at 0:42
@Jamex Also, again, what exactly you're trying to do is much more important than the speed of one specific operation. There's possibly an entirely different way to do whatever you want to accomplish that makes this question entirely obsolete. – deceze Sep 9 '11 at 0:44
Thanks guys, of course there are many approaches to coding. But for academic purpose, would it be faster to match/search (or whatever the correct method is) "xyz" in a large array or a string in php? I am not asking for specific applications, just whether you know in principle that one kind of storage is more efficient than the other. – Jamex Sep 9 '11 at 0:55
show 5 more comments

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.

share|improve this answer
Thanks, but my question is whether passing an array from JS to PHP without manipulation is technically possible, I know about JSON. I am now in the optimizing stage, as I have completed how I want the program to behave, unfortunately my program requires 20+ concurrent ajax calls to php, each call might require sending of a large 1000+ elements array or a long string of text (each call). The arrays are dynamic and will grow, which will only cause the program to become incrementally slower as a user continues along. – Jamex Sep 8 '11 at 23:29
2  
@Jamex perhaps your fundamental design may be improved. why would the info sent grow? are you appending to information that has already been sent to the server, thus resending duplicate information each time? why would you ever need to send 20 ajax calls to PHP concurrently, can you not put this all into one ajax call? what you are describing does not sound like it should ever be necessary. is you user creating 2000 new peices of information that need to be sent to the server? if not, are you sure that all of this needs to be sent? my guess is that there is another way to accomplish your goal. – dqhendricks Sep 8 '11 at 23:36

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.