Thanks for reading π
Follow @codedrops.tech for daily posts.
Instagram β Facebook
Micro Learning β Web Development β Javascript β MERN stack
www.codedrops.tech
Thanks for reading π
Follow @codedrops.tech for daily posts.
Instagram β Facebook
Micro Learning β Web Development β Javascript β MERN stack
www.codedrops.tech
For further actions, you may consider blocking this person and/or reporting abuse
Dominique Hosea -
Kshitij -
Michiharu Ono -
Onurhan -
Top comments (12)
Hi there, very nice use of the String & Array prototypes chains. Thanks for your solution.
If I may, I would like to suggest another alternative involving less time complexity and using a sentinel value for detecting spaces. Because using
split
andmap
andjoin
means many loops through the strings and array items generated.The following algorithm can reduce the time complexity to
O(n)
,n
being the number of characters in the text.I also run some performance tests to see the outcome and it seems like the solution you provided is around 40 to 50% slower than the one I provided. So performance in the case of capitalizing hundreds or thousands of strings should be taken into account I guess. It was tested on Google Chrome and may vary from a browser to another.
What do you think about it?
Just for the record: I'm not saying that your solution is wrong. In the contrary, I think that solution you provided is the best because it involves less complexity to find the perfect algorithm to solve the task and premature optimizations like I just did should never happen unless you have a very good reason to. What I showed is only for finding an alternative solution and discuss about it.
Also, you made a very minor typo by writing
capitlize
instead ofcapitalize
for the name of the function. But that's okay.Again, very good work mate!
Another alternative would be using a regular expression, which is quite nice and compact:
This also turns out to be even faster in my benchmarks - this will be due to the regular expression engine being highly optimised and built into the browser. My results from the
perf.link
site (which doesn't seem to want to let me copy a link directly to the benchmark for some reason...) are as follows:regexCapitalize
capitalize2
capitalize
That's awesome. Hell lot of differenceπ₯
This looks like a better version from a performance perspective for sure, I did this implementation of the same
In an article I wrote here a few days ago: dev.to/anders/why-do-we-write-java...
Given that "of" seems like it may be a little slow this is probably even faster.
Just on my phone right now but "split map join" in your perf-url is constantly 20-40% faster than the for loop version. I've done like 20 iterations now and number 2 is always faster (at least for me)
Lol. I made 2 typos actually. Got to know it now ππ thanks though..ππ
Sure, there are tradeoffs with every solution.
Case conversion can get a bit tricky with edge cases, like does "one.two" have one word or two, and what of words separated by tabs or newlines.
Often using a regular expression is the way to go.
Can I suggest something like this code, based on a small regex...
Regular expressions have a builtin assertion
\b
which can be used to detect word boundaries. The regex/\b[a-z]/
will match wherever the first letter of any word is lowercase. i.e. wherever a word boundary (i.e. not a letter, number or underscore) is followed by a lowercase letter.String.replace
takes a function that cleanly provides uppercased replacements for the matching letters.I'm guessing this will be faster, though I haven't done benchmarking. It certainly does fewer calls to
toUpperCase
I added your function to the benchmark site linked by @aminnairi
Benchmark hello world
Regex seems to performance the fastest... mostly. I'm not really sure how that site samples it's benchmark but it seems awfully fast, meaning few iterations? Having done roughly 30 iterations by hand your regex seems to be ~5-10% faster than the split-map-join. Sometimes the split-map-join is faster though, by a small margin. This is for "hello world" though. Once you start to use longer sentences, your regex becomes the clear winner by a huge margin (roughly 30-40% faster).
Benchmark with longer sentence
Hey thanks for that, that's awesome!
One nice thing about the regex way is that it targets the letters it needs to capitalize. I added test cases for an already capitalized sentence and
toTitleCase
gets a 400% speedup compared to the non-capitalized sentence (since it won't calltoUpperCase
if it doesn't need to). The others didn't get any speed up (their code could be tweaked, but I don't think there's a way to get such a boost).Benchmark here
I really liked this solution. Thanks for writing it.
Thanks Mehul