So this seems like a super basic question, but I'm wondering about javascript's built in .toUpperCase() method- is the time efficiency O(n) or O(1)?
I always just assumed it would be constant time, but now that I think about it ... "under the hood" wouldn't this method need to go through each character to check if it is lower case one by one? (therefore making it O(n)
The algorithm described in String.prototype.toLowerCase ( )
of the specification is O(n). Each codepoint of the original String (or String representation of the original object) is copied and converted to a new codepoint.
The fact that toUpperCase
and toLowerCase
return new Strings (because JavaScript Strings are immutable) is enough to verify that the operation takes linear time, not constant.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With