DEV Community

Discussion on: 1 line of code: How to camelCase a string

Collapse
 
xr0master profile image
Sergey Khomushin • Edited

Let's benchmark.

const toCamelCase = str => str.replace(/[\s\._-]+\w/g, (m) => m[m.length-1].toUpperCase());
Enter fullscreen mode Exit fullscreen mode

Or make a part of the string: 'to camel case'.toCamelCase();

String.prototype.toCamelCase = function() {return this.replace(/[\s\._-]+\w/g, (m) => m[m.length-1].toUpperCase())}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
martinkr profile image
Martin Krause

Benchmark
Half the time its the original, the other half the new version for me.

Collapse
 
xr0master profile image
Sergey Khomushin

Thanks. In Chrome, my version is 25% faster. It was obvious, though. Grouping is a heavy operation.

Thread Thread
 
martinkr profile image
Martin Krause

Thank you, I updated the article and the code.