From 9c35db38a0bc5ffab00c512d229deff5a3db9e9f Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Fri, 25 Sep 2015 16:15:40 -0700 Subject: [PATCH] Exit early in `baseToString` if `value` is already a string. --- lodash.js | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/lodash.js b/lodash.js index 88bbb681a..5cdd6ee8d 100644 --- a/lodash.js +++ b/lodash.js @@ -793,14 +793,18 @@ } /** - * Converts `value` to a string. An empty string is returned for `null` - * and `undefined` values. + * Converts `value` to a string if it's not one. + * An empty string is returned for `null` and `undefined` values. * * @private - * @param {*} value The value to convert. - * @returns {string} Returns the converted string. + * @param {*} value The value to process. + * @returns {string} Returns the string. */ function baseToString(value) { + // Exit early for strings to avoid a performance hit in some environments. + if (typeof value == 'string') { + return value; + } return value == null ? '' : (value + ''); } @@ -4523,7 +4527,7 @@ * Converts `value` to a function if it's not one. * * @private - * @param {*} value The value to convert. + * @param {*} value The value to process. * @returns {Function} Returns the function. */ function toFunction(value) { @@ -10526,7 +10530,7 @@ */ function endsWith(string, target, position) { string = baseToString(string); - target = (target + ''); + target = typeof target == 'string' ? target : (target + ''); var length = string.length; position = position === undefined