toSnakeCase

Benchmark created on


Setup

const strings = [
'IAmAString',
'This Is A String That Has Spaces',
'9StringsWouldBeALotToDoThisTo',
'WhatIfADigitLike6WereInTheMiddle',
'OrAtTheEnd8',
'IfANumberIsSurrounded8by6lowerCaseCharacters',
'OrAMultiDigitNumberLike77'
]

Test runner

Ready to run.

Testing in
TestOps/sec
Old Regex
strings.forEach((str) => str
    .match(/[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+/g)
    .map((x) => x.toLowerCase())
    .join('_'))

ready
New Thing
strings.forEach((str) => str
    .match(/[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+/g)
    .join('_')
    .toLowerCase())
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.