jsPerf.app is an online JavaScript performance benchmark test runner & jsperf.com mirror. It is a complete rewrite in homage to the once excellent jsperf.com now with hopefully a more modern & maintainable codebase.
jsperf.com URLs are mirrored at the same path, e.g:
https://jsperf.com/negative-modulo/2
Can be accessed at:
https://jsperf.app/negative-modulo/2
const x = "Performance about replace() or substr() in Javascript
Asked 11 years, 9 months ago
Modified 11 years, 9 months ago
Viewed 16k times
5
I was wondering about Javascript performance about using string.replace() or string.substr(). Let me explain what I'm doing.
I've a string like
str = "a.aa.a.aa."
I just have to "pop" last element in str where I always know what type of character it is (e.g, it's a dot here).
It's so simple, I can follow a lot of ways, like
str = str.substr(0, str.length-1) // same as using slice()
or
str = str.replace(/\.$/, '')
Which methods would you use? Why? Is there some lack in performance using this or that method? Length of the string is negligible.
(this is my first post, so if I'm doing something wrong please, notify me!)
javascriptregexreplaceslicesubstr
Share
Follow
asked Mar 6, 2012 at 15:31
Finalfire's user avatar
Finalfire
7311 gold badge22 silver badges77 bronze badges
Add a comment
5 Answers
Sorted by:
Highest score (default)
15
For performance tests in JavaScript use jsPerf.com
I created a testcase for your question here, which shows, that substr is a lot faster (at least in firefox).
Share
Follow
answered Mar 6, 2012 at 15:35
Sirko's user avatar
Sirko
72.9k1919 gold badges150150 silver badges184184 bronze badges
1
Just to chime in, I did the same, except I added in slice(0, -1) (which was slower). Logically, it's worth adding that we should expect a regular expression to be slower because at the very least it's adding in the step of testing parts of the string for matches. –
brymck
Mar 6, 2012 at 15:46
So cool! I forgot about [jsperf.com](jsPerf.com), I used it sometime ago. Anyway, thanks for the reply! –
Finalfire
Mar 6, 2012 at 17:09
But how can you replace a specific part of the string using substr? i.e. ` "my name is Hello World".replace('Hello', 'Hi'); ` , if you need to first get the indices of the start and end position, does that overhead compromise the performance boost of substr? –
benjaminz
Apr 8, 2016 at 14:49
@benjaminz you can easily take my test on jsperf and adapt it for your purpose. Then rerun it and see for yourself ,-) –
Sirko
Apr 8, 2016 at 15:30
Add a comment
Report this ad
2
If you just want the last character in the string, then use the subscript, not some replacement:
str[str.length-1]
Share
Follow
answered Mar 6, 2012 at 15:45
Phil H's user avatar
Phil H
20k77 gold badges6969 silver badges105105 bronze badges
Yeah I know but I have to modify the string, not only read last element. –
Finalfire
Mar 6, 2012 at 17:12
Add a comment
1
Do you have to do this thousands of times in a loop? If not (and "Length of string is negligible"), any way will do.
That said, I'd prefer the first option, since it makes the intention of trimming the last character more clear than the second one (oh, and it's faster, in case you do need to run this a zillion times. Since in the regex case, you need to not only build a new string but also compile a RegExp and run it against the input.)
Share
Follow
edited Mar 6, 2012 at 15:58
answered Mar 6, 2012 at 15:35
Alexander Pavlov's user avatar
Alexander Pavlov
31.7k55 gold badges6868 silver badges9393 bronze badges
@FlorianMargaine: amazing. For the regex solution, JS VM has to compile the regex, run it against the input string and build a new string. For the substr() solution, only building a new string is necessary. See Sirko's answer. –
Alexander Pavlov
Mar 6, 2012 at 15:46
My bad, I was actually wrong (thank god! regex are still slow.). You need to edit the answer so that I can un-downvote it, though. –
Florian Margaine
Mar 6, 2012 at 15:57
Sincerely I have to do it only one time in a row. Anyway, prefer to use first option too :) Thanks for reply. –
Finalfire
Mar 6, 2012 at 17:11
Add a comment
0
When you have this kind of doubt, either pick what you like the best (style-speaking, as running this only once doesn't matter much), or use http://jsperf.com.
For this very example, see here why substr is better :-).
Share
Follow
edited Mar 6, 2012 at 15:56
answered Mar 6, 2012 at 15:38
Florian Margaine's user avatar
Florian Margaine
59.2k1515 gold badges9191 silver badges118118 bronze badges
Add a comment
0
The substr way should always be faster than any kind of RegExp. But the performance difference should be minor.
Share
Follow
edited Mar 6, 2012 at 22:28
answered Mar 6, 2012 at 15:36
user1252065
Well just look at the jsPerf.com link by Sirko, it clearly shows that substr is faster... –
user1252065
Mar 6, 2012 at 15:45
Okay this is weird. Your posted jsPerf link is cleary in favor of regex, but Sirko's favors substr.Performance about replace() or substr() in Javascript
Asked 11 years, 9 months ago
Modified 11 years, 9 months ago
Viewed 16k times
5
I was wondering about Javascript performance about using string.replace() or string.substr(). Let me explain what I'm doing.
I've a string like
str = "a.aa.a.aa."
I just have to "pop" last element in str where I always know what type of character it is (e.g, it's a dot here).
It's so simple, I can follow a lot of ways, like
str = str.substr(0, str.length-1) // same as using slice()
or
str = str.replace(/\.$/, '')
Which methods would you use? Why? Is there some lack in performance using this or that method? Length of the string is negligible.
(this is my first post, so if I'm doing something wrong please, notify me!)
javascriptregexreplaceslicesubstr
Share
Follow
asked Mar 6, 2012 at 15:31
Finalfire's user avatar
Finalfire
7311 gold badge22 silver badges77 bronze badges
Add a comment
5 Answers
Sorted by:
Highest score (default)
15
For performance tests in JavaScript use jsPerf.com
I created a testcase for your question here, which shows, that substr is a lot faster (at least in firefox).
Share
Follow
answered Mar 6, 2012 at 15:35
Sirko's user avatar
Sirko
72.9k1919 gold badges150150 silver badges184184 bronze badges
1
Just to chime in, I did the same, except I added in slice(0, -1) (which was slower). Logically, it's worth adding that we should expect a regular expression to be slower because at the very least it's adding in the step of testing parts of the string for matches. –
brymck
Mar 6, 2012 at 15:46
So cool! I forgot about [jsperf.com](jsPerf.com), I used it sometime ago. Anyway, thanks for the reply! –
Finalfire
Mar 6, 2012 at 17:09
But how can you replace a specific part of the string using substr? i.e. ` "my name is Hello World".replace('Hello', 'Hi'); ` , if you need to first get the indices of the start and end position, does that overhead compromise the performance boost of substr? –
benjaminz
Apr 8, 2016 at 14:49
@benjaminz you can easily take my test on jsperf and adapt it for your purpose. Then rerun it and see for yourself ,-) –
Sirko
Apr 8, 2016 at 15:30
Add a comment
Report this ad
2
If you just want the last character in the string, then use the subscript, not some replacement:
str[str.length-1]
Share
Follow
answered Mar 6, 2012 at 15:45
Phil H's user avatar
Phil H
20k77 gold badges6969 silver badges105105 bronze badges
Yeah I know but I have to modify the string, not only read last element. –
Finalfire
Mar 6, 2012 at 17:12
Add a comment
1
Do you have to do this thousands of times in a loop? If not (and "Length of string is negligible"), any way will do.
That said, I'd prefer the first option, since it makes the intention of trimming the last character more clear than the second one (oh, and it's faster, in case you do need to run this a zillion times. Since in the regex case, you need to not only build a new string but also compile a RegExp and run it against the input.)
Share
Follow
edited Mar 6, 2012 at 15:58
answered Mar 6, 2012 at 15:35
Alexander Pavlov's user avatar
Alexander Pavlov
31.7k55 gold badges6868 silver badges9393 bronze badges
@FlorianMargaine: amazing. For the regex solution, JS VM has to compile the regex, run it against the input string and build a new string. For the substr() solution, only building a new string is necessary. See Sirko's answer. –
Alexander Pavlov
Mar 6, 2012 at 15:46
My bad, I was actually wrong (thank god! regex are still slow.). You need to edit the answer so that I can un-downvote it, though. –
Florian Margaine
Mar 6, 2012 at 15:57
Sincerely I have to do it only one time in a row. Anyway, prefer to use first option too :) Thanks for reply. –
Finalfire
Mar 6, 2012 at 17:11
Add a comment
0
When you have this kind of doubt, either pick what you like the best (style-speaking, as running this only once doesn't matter much), or use http://jsperf.com.
For this very example, see here why substr is better :-).
Share
Follow
edited Mar 6, 2012 at 15:56
answered Mar 6, 2012 at 15:38
Florian Margaine's user avatar
Florian Margaine
59.2k1515 gold badges9191 silver badges118118 bronze badges
Add a comment
0
The substr way should always be faster than any kind of RegExp. But the performance difference should be minor.
Share
Follow
edited Mar 6, 2012 at 22:28
answered Mar 6, 2012 at 15:36
user1252065
Well just look at the jsPerf.com link by Sirko, it clearly shows that substr is faster... –
user1252065
Mar 6, 2012 at 15:45
Okay this is weird. Your posted jsPerf link is cleary in favor of regex, but Sirko's favors substr.Performance about replace() or substr() in Javascript
Asked 11 years, 9 months ago
Modified 11 years, 9 months ago
Viewed 16k times
5
I was wondering about Javascript performance about using string.replace() or string.substr(). Let me explain what I'm doing.
I've a string like
str = "a.aa.a.aa."
I just have to "pop" last element in str where I always know what type of character it is (e.g, it's a dot here).
It's so simple, I can follow a lot of ways, like
str = str.substr(0, str.length-1) // same as using slice()
or
str = str.replace(/\.$/, '')
Which methods would you use? Why? Is there some lack in performance using this or that method? Length of the string is negligible.
(this is my first post, so if I'm doing something wrong please, notify me!)
javascriptregexreplaceslicesubstr
Share
Follow
asked Mar 6, 2012 at 15:31
Finalfire's user avatar
Finalfire
7311 gold badge22 silver badges77 bronze badges
Add a comment
5 Answers
Sorted by:
Highest score (default)
15
For performance tests in JavaScript use jsPerf.com
I created a testcase for your question here, which shows, that substr is a lot faster (at least in firefox).
Share
Follow
answered Mar 6, 2012 at 15:35
Sirko's user avatar
Sirko
72.9k1919 gold badges150150 silver badges184184 bronze badges
1
Just to chime in, I did the same, except I added in slice(0, -1) (which was slower). Logically, it's worth adding that we should expect a regular expression to be slower because at the very least it's adding in the step of testing parts of the string for matches. –
brymck
Mar 6, 2012 at 15:46
So cool! I forgot about [jsperf.com](jsPerf.com), I used it sometime ago. Anyway, thanks for the reply! –
Finalfire
Mar 6, 2012 at 17:09
But how can you replace a specific part of the string using substr? i.e. ` "my name is Hello World".replace('Hello', 'Hi'); ` , if you need to first get the indices of the start and end position, does that overhead compromise the performance boost of substr? –
benjaminz
Apr 8, 2016 at 14:49
@benjaminz you can easily take my test on jsperf and adapt it for your purpose. Then rerun it and see for yourself ,-) –
Sirko
Apr 8, 2016 at 15:30
Add a comment
Report this ad
2
If you just want the last character in the string, then use the subscript, not some replacement:
str[str.length-1]
Share
Follow
answered Mar 6, 2012 at 15:45
Phil H's user avatar
Phil H
20k77 gold badges6969 silver badges105105 bronze badges
Yeah I know but I have to modify the string, not only read last element. –
Finalfire
Mar 6, 2012 at 17:12
Add a comment
1
Do you have to do this thousands of times in a loop? If not (and "Length of string is negligible"), any way will do.
That said, I'd prefer the first option, since it makes the intention of trimming the last character more clear than the second one (oh, and it's faster, in case you do need to run this a zillion times. Since in the regex case, you need to not only build a new string but also compile a RegExp and run it against the input.)
Share
Follow
edited Mar 6, 2012 at 15:58
answered Mar 6, 2012 at 15:35
Alexander Pavlov's user avatar
Alexander Pavlov
31.7k55 gold badges6868 silver badges9393 bronze badges
@FlorianMargaine: amazing. For the regex solution, JS VM has to compile the regex, run it against the input string and build a new string. For the substr() solution, only building a new string is necessary. See Sirko's answer. –
Alexander Pavlov
Mar 6, 2012 at 15:46
My bad, I was actually wrong (thank god! regex are still slow.). You need to edit the answer so that I can un-downvote it, though. –
Florian Margaine
Mar 6, 2012 at 15:57
Sincerely I have to do it only one time in a row. Anyway, prefer to use first option too :) Thanks for reply. –
Finalfire
Mar 6, 2012 at 17:11
Add a comment
0
When you have this kind of doubt, either pick what you like the best (style-speaking, as running this only once doesn't matter much), or use http://jsperf.com.
For this very example, see here why substr is better :-).
Share
Follow
edited Mar 6, 2012 at 15:56
answered Mar 6, 2012 at 15:38
Florian Margaine's user avatar
Florian Margaine
59.2k1515 gold badges9191 silver badges118118 bronze badges
Add a comment
0
The substr way should always be faster than any kind of RegExp. But the performance difference should be minor.
Share
Follow
edited Mar 6, 2012 at 22:28
answered Mar 6, 2012 at 15:36
user1252065
Well just look at the jsPerf.com link by Sirko, it clearly shows that substr is faster... –
user1252065
Mar 6, 2012 at 15:45
Okay this is weird. Your posted jsPerf link is cleary in favor of regex, but Sirko's favors substr."
Ready to run.
Test | Ops/sec | |
---|---|---|
Replace |
| ready |
two |
| ready |
You can edit these tests or add more tests to this page by appending /edit to the URL.