Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Run results for:
stringPrettify
Go to the benchmark
Embed
Embed Benchmark Result
Run details:
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36
Browser:
Chrome 131
Operating system:
Windows
Device Platform:
Desktop
Date tested:
one year ago
Test name
Executions per second
Cycle for & charCode
3705731.5 Ops/sec
Regex
1185343.1 Ops/sec
HTML Preparation code:
<!--your preparation HTML code goes here-->
Script Preparation code:
function stringPrettify(text, capitalize = true, handleKebabCase = false) { let r = ""; let nextUpper = false; let wasUpper = false; for (let i = 0; i < text.length; ++i) { const c = text.charCodeAt(i); if (c > 96 && c < 123) { wasUpper = false; r += String.fromCharCode(nextUpper || i === 0 ? c - 32 : c); } else if (c > 64 && c < 91) { const cNext = text.charCodeAt(i + 1); const isAbbr = i === text.length - 1 || (cNext > 64 && cNext < 91); const isAbbrPrev = wasUpper && isAbbr; if (!nextUpper && !isAbbrPrev && i !== 0) { r += " "; } r += String.fromCharCode(capitalize || isAbbr || i === 0 ? c : c + 32); wasUpper = true; } else if (c === 95 || (c === 45 && handleKebabCase)) { wasUpper = false; if (i !== 0) { r += " "; } nextUpper = capitalize; continue; } else { r += String.fromCharCode(c); wasUpper = false; } nextUpper = false; } return r; } function stringPrettifyOld(text, capitalize = true) { const r = text .replace(/([A-ZА-Я])/g, " $1") .trimStart() .replace(new RegExp(`[_${(capitalize && "-") || ""}]`, "g"), " ") .replace(/[ ]{2,}/, " "); return r.charAt(0).toUpperCase() + r.slice(1); }
Tests:
Cycle for & charCode
let t = stringPrettify('somePropValue'); let t2 = stringPrettify('some_prop_value'); let t3 = stringPrettify('some-prop-value');
Regex
let t = stringPrettifyOld('somePropValue'); let t2 = stringPrettifyOld('some_prop_value'); let t3 = stringPrettifyOld('some-prop-value');