1

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:

  1. regex: "string".replace(/s/g, "");
  2. split-join: "string".split("s").join("")

Which of those variants is better? (persistence, time etc.)

I prefer the second one because of its readability.

Cœur
39k25 gold badges207 silver badges282 bronze badges
asked Jan 18, 2018 at 6:55
2
  • 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. Commented 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 one Commented Jan 18, 2018 at 6:57

3 Answers 3

1

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.

answered Jan 18, 2018 at 6:58
Sign up to request clarification or add additional context in comments.

2 Comments

I expected that answer ... But i can't get into the "regex" thing. I always have to look them up or check them on regexer.
But nowadays those regex testers make it a breeze learning regex. Not only can you test and see an immediate result, they also will explain the syntax on the side! The faster you learn this, the better. There's no proper way around it! ;-)
1

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.

answered Jan 18, 2018 at 13:38

Comments

1

"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.

answered Jan 18, 2018 at 7:12

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.