Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Questions on Speed for String Parsing/Comparisons  RSS feed

 
Robert Paris
Ranch Hand
Posts: 585
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Many times I find I have a need to parse a string, looking for another string value inside, or compare strings. I know that String actions eat alot of time/resources, so I'm looking for a way to speed this up. One idea I had was to convert all my Strings to byte[] objects, but I have no idea if this is faster, nor if it's the quickest way to do this. Does anyone know the best way?
For example, let's say I have a String that comes in from an email that is this:
"Hey Robert,\r\nWhat's been going on?\r\n"
I want to find all the cases of "\r\n" or, if the program that sent the message used "\n" instead of "\r\n", then i want to find all those. What's the fastest way to do this?
Thanks in advance!
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, you can expect the String methods to already be maximally optimized, so there really is no reason to do something like a conversion to byte[] or the like (String *is* working on char[] internally).
In some cases, intelligent use of regular expressions might be more effective than indexOf() and the like, but it's hard to say in general.
Anyway, I would suggest using a profiler first to get more info on where your performance bottlenecks are - you might be surprised...
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!