Anybody have thoughts on the performance of the fo...
# announcements
n
Anybody have thoughts on the performance of the following code block? I came across this in our project and I feel like this would not perform well
Copy code
inline fun <reified T> HashMap<String, T>.toAnalyticsBundle(): Bundle {
    val analyticsRegex = Regex(FirebaseAnalyticsManager.ANALYTICS_REGEX_STRING)

    return Bundle().apply {
        for (key in keys) {
            val value = this@toAnalyticsBundle[key]
            val firebaseKey = FirebaseAnalyticsManager.googleAnalyticsifyString(key, analyticsRegex)
            when (value) {
                is Int -> this@apply.putInt(firebaseKey, value)
                is Float -> this@apply.putFloat(firebaseKey, value)
                is Double -> this@apply.putDouble(firebaseKey, value)
                is Boolean -> this@apply.putBoolean(firebaseKey, value)
                is Char -> this@apply.putChar(firebaseKey, value)
                is String -> this@apply.putString(firebaseKey, value)
                is Parcelable -> this@apply.putParcelable(firebaseKey, value)
                is Serializable -> this@apply.putSerializable(firebaseKey, value)
            }

        }
    }
}
fun googleAnalyticsifyString(stringToConvert: String, regex: Regex = Regex(ANALYTICS_REGEX_STRING)): String {
    val charArray = stringToConvert.toLowerCase().replace(regex, "").toCharArray()
    for((index, char) in charArray.withIndex()) {
        if(char == ' ') {
            val nextCharIndex = index + 1
            if(nextCharIndex < stringToConvert.length) {
                charArray[nextCharIndex] = charArray[nextCharIndex].toUpperCase()
            }
        }
    }
    return charArray
        .joinToString("")
        .replace(" ", "")
}
g
I would suggest to write a test, feed it lots of input and measure the performance. No need to optimize code if there’s no problem.
☝️ 3