1 person found this helpful
var myContents = myContents.replace(/ +/g," ").replace(/\|vertsb\|/g,"").replace(/\|icrl\|/g,"").replace(/\|wspon\|/g,"");
That looks promising, before I move my hundreds of searches to one huge line, do you know if it makes a difference in performance?
1 person found this helpful
You can replace three replaces with one:
var myContents = myContents.replace(/\|vertsb\||\|icrl\||\|wspon\|/g,"")
That is true and helpful, but the vast majority of these are getting replaced with different characters.
Unless the string is very large, I don't think the extra variables will affect performance that much. Do some testing, and let us know...
Alright, if we don't here from someone with experience in the
issue of storing the varable I will try it and let you know.
You should create the variable just once:
var myContents = myContents.replace(/ +/g," ");
myContents = myContents.replace(/\|vertsb\|/g,"");
myContents = myContents.replace(/\|icrl\|/g,"");
myContents = myContents.replace(/\|wspon\|/g,"");
This may not make much difference in performance, but it's generally considered good practice to create variables just once.
What *will* make a difference is a change in the first line. You now have this (using \x20 for the space to make it better visible):
var myContents = myContents.replace(/\x20+/g," ");
which paraphrases as "replace every sequence of one or more spaces with a single space". But what you want is "replace every sequence of two or more spaces with one space":
var myContents = myContents.replace(/\x20\x20+/g," ");
Thanks Peter, you are correct, changing to double space made a big difference, thanks!
Ok, the results are in. Nothing made a significant difference either way, but both ideas actually consisntently took a fraction (I mean literally a fraction) longer.
I combined 18 searches into 1 using Harbs suggestion and it went (per document) from 120 to 132 milliseconds (just those 18 searches).
I combined 60 searches using tomaxxi's suggestion and it went from 210 to 212-215 milliseconds (just those 60 searches).
I used the JS date object to time it.
I think what I have learned from this is not to worry about overwriting a variable ;-).
Here is something more interesting though:
Before I transferred these searches into JS I was using InDesign's findGrep().
Here is the before and after in CS3 and CS4, this is for about 200 searches (in seconds):
CS3 using findGrep(): 3.257
CS4 using findGrep(): 20.025
CS5 using findGrep(): 74.523
CS3 using JS: 0.461
CS4 using JS: 0.607
What happened that Grep searches are taking 7 times as long in CS4 vs. CS3? And CS5 is walking in the footsteps of its predecessor by taking over a minute for something that used to take 3 seconds in CS3. What is going on? This is important for searches that can't be done with JS, i.e. formatted text.
After further testing I see the problem is only if the text is *not* overset.