Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
128
rated 0 times [  131] [ 3]  / answers: 1 / hits: 15341  / 13 Years ago, tue, march 6, 2012, 12:00:00

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!)


More From » regex

 Answers
67

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


[#87028] Sunday, March 4, 2012, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
johannatorim

Total Points: 599
Total Questions: 124
Total Answers: 100

Location: Virgin Islands (U.S.)
Member since Fri, May 7, 2021
3 Years ago
;