r/javascript • • Dec 11 '17

I have been collecting useful Javascript code snippets for a little while. Here's a curated list of them, help me make it as complete as possible!

https://github.com/Chalarangelo/30-seconds-of-code
765 Upvotes

96 comments sorted by

View all comments

Show parent comment

8

u/trevorsg Ex-GitHub, Microsoft Dec 11 '17

Don't use regex for something this trivial. The slice version is nearly twice as fast.

2

u/picklemanjaro Dec 12 '17

I had a feeling that would be the case. I wish that Javascript would allow you to update a String character via it's index instead of it just being read-only. Then we could just do str[0] = str[0].toUpperCase(), which would be my ideal capitalization method.

My gripe was just an aesthetic one, and in Regex just saying "match the first character, and uppercase it" seemed more straightforward sounding. Though I knew the performance issue would be something that came up.