Regex Replace vs. Slice

Benchmark created on


Test runner

Ready to run.

Testing in
TestOps/sec
Slice
const trailingSlashPath = 'a/long/long/long/long/url/'

const trailingWildcardPath =  'a/long/long/long/long/url/*'

const formattedPathTrailingSlash = trailingSlashPath[trailingSlashPath.length - 1] === '/'
      ? trailingSlashPath.slice(0, -1)
      : trailingSlashPath;
        
const formattedPathTrailingWildCard = trailingWildcardPath.slice(-2) === '/*'
        ? trailingWildcardPath.slice(0, -1)
        : trailingWildcardPath;


ready
Regex Replace
const trailingSlashPath = 'a/long/long/long/long/url/'

const trailingWildcardPath =  'a/long/long/long/long/url/*'

const formattedPathSlash = trailingSlashPath.replace(/\/$/, "");
const formattedPathWildcard = trailingWildcardPath.replace(/\*$/,"");

ready

Revisions

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