Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Run results for:
Normalize path: JS Regex vs .endsWith vs .indexOf vs .slice
Go to the benchmark
Embed
Embed Benchmark Result
Run details:
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36
Browser:
Chrome 115
Operating system:
Linux
Device Platform:
Desktop
Date tested:
2 years ago
Test name
Executions per second
regex replace normalize
10.0 Ops/sec
endsWith slice normalize
259.1 Ops/sec
slice slice normalize
332.0 Ops/sec
lastIndexOf slice normalize
128.8 Ops/sec
HTML Preparation code:
<div></div>
Script Preparation code:
var data = window.data = []; const possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789."; var TOTAL_STRINGS = window.TOTAL_STRINGS = 100000; function getRandomInt(max) { return Math.floor(Math.random() * max); } function makeRandomString(len) { var text = ""; for (var i = 0; i < len; i++) { text += possible.charAt(getRandomInt(possible.length)); } return text; } function makeRandomUrl(len) { var url = "file:///"; for (var i = 0; i < len; i++) { url += makeRandomString(5 + getRandomInt(10)); if (getRandomInt(2)) { url += "/"; } } return url; } while (data.length < TOTAL_STRINGS) { data.push(makeRandomUrl(getRandomInt(10))); }
Tests:
regex replace normalize
var x = 0; var TOTAL_STRINGS = window.TOTAL_STRINGS; var data = window.data; while (x < TOTAL_STRINGS) { const str = data[x]; const url = str.replace(/\/?$/, ''); x += 1; }
endsWith slice normalize
var x = 0; var TOTAL_STRINGS = window.TOTAL_STRINGS; var data = window.data; while (x < TOTAL_STRINGS) { const str = data[x]; const url = str.endsWith("/") ? str.slice(0, -1) : str; x += 1; }
slice slice normalize
var x = 0; var TOTAL_STRINGS = window.TOTAL_STRINGS; var data = window.data; while (x < TOTAL_STRINGS) { const str = data[x]; const url = str.slice(-1) === "/" ? str.slice(0, -1) : str; x += 1; }
lastIndexOf slice normalize
var x = 0; var TOTAL_STRINGS = window.TOTAL_STRINGS; var data = window.data; while (x < TOTAL_STRINGS) { const str = data[x]; const url = str.lastIndexOf("/") === str.length - 1 ? str.slice(0, -1) : str; x += 1; }