write an efficient js function that turns camel fooBar to 'foo-bar` case. regex is not…
- User
write an efficient js function that turns camel fooBar to 'foo-bar` case. regex is not fast.
- Assistant
write an efficient js function that turns camel fooBar to 'foo-bar` case. regex is not fast.
Certainly! Converting camelCase strings (like fooBar) to kebab-case (foo-bar) efficiently without using regular expressions can be achieved by iterating through each character of the string. This approach minimizes overhead and is generally faster, especially for longer strings.
Here's a performant JavaScript function to do just that:
/**
* Converts a camelCase string to kebab-case.
*
* @param {string} str - The camelCase string to convert.
* @returns {string} The kebab-case converted string.
*/
function camelToKebab(str) {
if (!str) return '';
const length = str.length;
let result = '';
result += str[0].toLowerCase(); // Initialize with the first character in lowercase
for (let i = 1; i < length; i++) {
const char = str[i];
const lowerChar = char.toLowerCase();
// Check if the character is an uppercase letter
if (char !== lowerChar) {
result += '-' + lowerChar;
} else {
result += char;
}
}
return result;
}
// Example usage:
console.log(camelToKebab('fooBar')); // Output: "foo-bar"
console.log(camelToKebab('FooBarBaz')); // Output: "foo-bar-baz"
console.log(camelToKebab('convertThisNow')); // Output: "convert-this-now"
Initialization:
result string with the first character of the input string converted to lowercase. This handles cases where the string may start with an uppercase letter.Iteration:
-) followed by the lowercase version of that character to the result string.Return:
result string in kebab-case.Avoiding Regex: Regular expressions can introduce performance overhead, especially with complex patterns or very long strings. By using a simple loop, we ensure that the conversion is as efficient as possible.
Single Pass: The function makes a single pass through the input string, ensuring linear time complexity (O(n)), where n is the length of the string.
String Concatenation: While string concatenation inside a loop can sometimes be less efficient in certain languages, modern JavaScript engines optimize string operations effectively. However, for extremely long strings, you might consider using an array to collect characters and then joining them at the end, as shown below:
function camelToKebab(str) {
if (!str) return '';
const length = str.length;
const chars = [];
chars.push(str[0].toLowerCase());
for (let i = 1; i < length; i++) {
const char = str[i];
const lowerChar = char.toLowerCase();
if (char !== lowerChar) {
chars.push('-', lowerChar);
} else {
chars.push(char);
}
}
return chars.join('');
}
This alternative approach can offer slight performance benefits for very long strings by minimizing the number of intermediate string creations.
The provided function offers an efficient way to convert camelCase to kebab-case without relying on regular expressions. It ensures good performance even with longer strings by using straightforward character checks and minimal operations within a single iteration.