<div></div>
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)));
}
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;
}
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;
}
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;
}
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;
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
regex replace normalize | |
endsWith slice normalize | |
slice slice normalize | |
lastIndexOf slice normalize |
Test name | Executions per second |
---|---|
regex replace normalize | 75.7 Ops/sec |
endsWith slice normalize | 109.7 Ops/sec |
slice slice normalize | 96.4 Ops/sec |
lastIndexOf slice normalize | 65.6 Ops/sec |
Benchmark Overview
The provided benchmark compares the performance of three different approaches to normalize path strings: regular expression (regex
), endsWith
, and slice
. The test creates a large dataset of random URLs, which are then processed using each normalization approach.
Options Compared
replace()
method with a regular expression to remove the trailing slash from the URL.endsWith()
method and replaces it with an empty string if true.slice()
method to remove the last character of the URL (the trailing slash) if it exists.Pros and Cons
Library Usage
None of the options explicitly use any external libraries.
Special JS Features/Syntax
The benchmark uses various modern JavaScript features, including:
x => { ... }
)const url = 'file:///';
)url += makeRandomString(5 + getRandomInt(10))
)These features are not critical to the understanding of the benchmark's purpose and comparisons.
Alternatives
Other alternatives for normalizing path strings could include:
path-normalize
or normalize-url
.stream
.However, the benchmark's focus on performance and simplicity makes the three approaches being compared (regex, endsWith
, and slice
) particularly relevant for understanding optimization strategies in JavaScript.