10
I need to do some bulk replacement operations with Javascript. For my specific problem, let’s assume that I need to replace a specific substring. I need to replace each occurrence of "blue" with "green".
So I have two ways of doing:
var foo = pegaTexto(); // imagine que isso retorna uma string qualquer;
foo = foo.replace("azul", "verde");
Or:
var foo = pegaTexto();
foo = foo.replace(new RegExp("azul"), "verde");
In my view, in such a situation, the only practical use of using a regular expression would be to specify a flag (to say for example whether the search should be global), but in my case I am omitting the flags of the expression builder.
I have one question, however, regarding the performance of these two distinct forms.
Forcing the use of regular expression has some impact on performance?
How can I measure it?
http://jsperf.com/teste-de-replace
– Sergio
@Sergio thank you very much! This is worth an answer, if you do, I will be happy to score and mark as correct :)
– Oralista de Sistemas