jsPerf.app is an online JavaScript performance benchmark test runner & jsperf.com mirror. It is a complete rewrite in homage to the once excellent jsperf.com now with hopefully a more modern & maintainable codebase.
jsperf.com URLs are mirrored at the same path, e.g:
https://jsperf.com/negative-modulo/2
Can be accessed at:
https://jsperf.app/negative-modulo/2
Test the speed of various methods to camel case a string. http://stackoverflow.com/questions/2970525/javascript-regex-camel-case-sentence-case
<script>
String.prototype.toCamelCase = function() {
return this
.replace(/\s(.)/g, function($1) { return $1.toUpperCase(); })
.replace(/\s/g, '')
.replace(/^(.)/, function($1) { return $1.toLowerCase(); });
};
String.prototype.camelizeOne = function() {
return this.replace(/(?:^\w|[A-Z]|\b\w)/g, function(letter, index) {
return index == 0 ? letter.toLowerCase() : letter.toUpperCase();
}).replace(/\s+/g, '');
};
String.prototype.camelizeTwo = function() {
return this.replace(/(?:^\w|[A-Z]|\b\w|\s+)/g, function(match, index) {
if (+match === 0) return ""; // or if (/\s+/.test(match)) for white spaces
return index == 0 ? match.toLowerCase() : match.toUpperCase();
});
};
String.prototype.toUpperCaseFirstChar = function() {
return this.substr( 0, 1 ).toUpperCase() + this.substr( 1 );
};
String.prototype.toLowerCaseFirstChar = function() {
return this.substr( 0, 1 ).toLowerCase() + this.substr( 1 );
};
String.prototype.toUpperCaseEachWord = function( delim ) {
delim = delim ? delim : ' ';
return this.split( delim ).map( function(v) { return v.toUpperCaseFirstChar() } ).join( delim );
};
String.prototype.toLowerCaseEachWord = function( delim ) {
delim = delim ? delim : ' ';
return this.split( delim ).map( function(v) { return v.toLowerCaseFirstChar() } ).join( delim );
};
var string = "How can I convert a string into camel case using javascript regex?";
var otherCase = "BeepBoopBeep";
</script>
Ready to run.
Test | Ops/sec | |
---|---|---|
toCamelCase |
| ready |
camelizeOne |
| ready |
camelizeTwo |
| ready |
otherCase toCamelCase |
| ready |
otherCase camelizeOne |
| ready |
otherCase camelizeTwo |
| ready |
noregex |
| ready |
otherCase noregex |
| ready |
You can edit these tests or add more tests to this page by appending /edit to the URL.