Skip to content

Instantly share code, notes, and snippets.

@brandonvanha
Last active August 29, 2015 14:08
Show Gist options
  • Save brandonvanha/910b8e379cbab4d4c0c4 to your computer and use it in GitHub Desktop.
Save brandonvanha/910b8e379cbab4d4c0c4 to your computer and use it in GitHub Desktop.
Efficient Codes
• 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