2

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.

Jamex
  • 1,164
  • 5
  • 22
  • 34

2 Answers2

3

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!

deceze
  • 510,633
  • 85
  • 743
  • 889
  • 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 08 '11 at 23:38
  • @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 09 '11 at 00: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 09 '11 at 00: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 09 '11 at 00:55
  • Thanks for the effort DC, I am amazed that these languages don't have some sort of benchmarks to differentiated strings and arrays. – Jamex Sep 09 '11 at 03:02
  • @Jamex What do you mean by that? O_o? – deceze Sep 09 '11 at 03:08
  • Hi, I mean that JS or PHP or any language should have some sort of common knowledge that (for example) strings can be handled at faster speed than arrays if compare small strings to small arrays and vice versa. – Jamex Sep 09 '11 at 05:20
  • @Jamex For PHP that may be possible, since there's pretty much only one canonical implementation of it. But even in PHP the speed of some things depends on the underlying OS, so may behave differently on different servers. Javascript doesn't have *one* canonical implementation; it's a specification (ECMAScript) that has *dozens* of implementations. Of course there are differences among them. – deceze Sep 09 '11 at 05:25
0

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.

dqhendricks
  • 19,030
  • 11
  • 50
  • 83
  • 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 08 '11 at 23:29
  • 3
    @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 08 '11 at 23:36