Last active
August 29, 2015 14:08
-
-
Save brandonvanha/910b8e379cbab4d4c0c4 to your computer and use it in GitHub Desktop.
Efficient Codes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
• Apply the array method map() to a string: | |
> [].map.call('abc', function (x) { return x.toUpperCase() }) | |
[ 'A', 'B', 'C' ] | |
Using map() generically is more efficient than using split(''), which creates an intermediate array: | |
> 'abc'.split('').map(function (x) { return x.toUpperCase() }) | |
[ 'A', 'B', 'C' ] | |
Apply a string method to nonstrings. toUpperCase() converts the receiver to a string and uppercases the result: | |
> String.prototype.toUpperCase.call(true) | |
'TRUE' | |
> String.prototype.toUpperCase.call(['a','b','c']) | |
'A,B,C' | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment