I'm currently writing a simple converter in javascript where I have to replace a couple of strings.
I know that you can replace those by using:
- regex:
"string".replace(/s/g, ""); - split-join:
"string".split("s").join("")
Which of those variants is better? (persistence, time etc.)
I prefer the second one because of its readability.
-
Not an answer, but I definitely don't prefer the second variant. For one, in my mind it's harder to read, because it has two steps, instead of one. Also, I would expect that the cost of splitting and the recombining would be higher than just walking down the string once and doing several in place replacements.Tim Biegeleisen– Tim Biegeleisen2018年01月18日 06:57:54 +00:00Commented Jan 18, 2018 at 6:57
-
I would prefer to go with the first one, since on an inital thought, the second one has two function call to obtain the result, in case the first one does it in oneAswin Ramesh– Aswin Ramesh2018年01月18日 06:57:55 +00:00Commented Jan 18, 2018 at 6:57
3 Answers 3
Regexes are almost always faster; in this case especially: you are building an array from a string, which can be costly depending on the size of the string. You can easily test this youself with a few different strings and few thousand runs. I'd clearly recommend the regex though.
2 Comments
Here is a benchmark (in perl, but it will be similar in javascript):
#!/usr/bin/perl
use Benchmark qw(:all);
my $str = "string";
my $count = -3;
cmpthese($count, {
'split.join' => sub {
$str = join'',split's',$str;
},
'regex' => sub {
$str =~ s/s//g;
},
});
Output:
Rate split.join regex
split.join 5068934/s -- -76%
regex 20811069/s 311% --
As you can see, regex is about 4 times quicker than consecutive 2 functions split and join.
Comments
"Better" is relative. You have to define it.
Memory-wise less allocations is always better. Computation-wise, hard-coded solutions generally provide better results. All operations takes some time, but in JS it depends on the virtual machine that script runs.
If provided code will be called 1000 times a day per user, then it really does not matter. You cannot achieve "better" performance.
If you are after "better" splitting, than regex is your buddy. In this case "better" means "unicode support", "non-latin character support", "less code" and of course some difficult to code operation like splitting by uppercase letters only if they are after a lowercase letter.
RegEx is a language, in my humble opinion, every programmer must learn. Basic syntax is enough for most operations.
Imagine how many lines you would have to write to find a letter in a string repeated three or more times consecutively. RegEx is \w1円1円 and you are done.