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 and map and join 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 of capitalize for the name of the function. But that's okay.
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)
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:
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!
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.
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.
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๐ฅ