Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Run results for:
Extension stripping
Benchmarks different approaches to stripping a file extension from a path string
Go to the benchmark
Embed
Embed Benchmark Result
Run details:
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36
Browser:
Chrome 127
Operating system:
Mac OS X 10.15.7
Device Platform:
Desktop
Date tested:
one year ago
Test name
Executions per second
RegExp string replace
3479855.5 Ops/sec
lastIndexOf and string.slice
7623170.0 Ops/sec
Tests:
RegExp string replace
const stripExtension = (path) => path.replace(/\.\w+$/, ''); stripExtension('short') stripExtension('with.ext') stripExtension('longer/path/with-segments/and/an-extension.png')
lastIndexOf and string.slice
const stripExtension = (path) => { const periodIndex = path.lastIndexOf('.'); return periodIndex < path.length - 1 ? path.slice(0, periodIndex) : path; } stripExtension('short') stripExtension('with.ext') stripExtension('longer/path/with-segments/and/an-extension.png')
lastIndexOf and string.substring
const stripExtension = (path) => { const periodIndex = path.lastIndexOf('.'); return periodIndex < path.length - 1 ? path.substring(0, periodIndex) : path; } stripExtension('short') stripExtension('with.ext') stripExtension('longer/path/with-segments/and/an-extension.png')
lastIndexOf and regexp test on rest
const extRE = /^\.\w+$/; const stripExtension = (path) => { const periodIndex = path.lastIndexOf('.'); const rest = path.slice(periodIndex); return rest && extRE.test(rest) ? path.slice(0, periodIndex) : path; } stripExtension('short') stripExtension('with.ext') stripExtension('longer/path/with-segments/and/an-extension.png')