Skip to content

Commit a5b0d89

Browse files
gurgundaymarco-ippolito
authored andcommittedJan 24, 2025
lib: remove startsWith/endsWith primordials for char checks
PR-URL: #55407 Reviewed-By: Jacob Smith <jacob@frende.me> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Mattias Buelens <mattias@buelens.com> Reviewed-By: Ethan Arrowood <ethan@arrowood.dev>
1 parent 50b6729 commit a5b0d89

File tree

11 files changed

+17
-22
lines changed

11 files changed

+17
-22
lines changed
 

‎lib/_http_agent.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ function calculateServerName(options, req) {
347347
// abc:123 => abc
348348
// [::1] => ::1
349349
// [::1]:123 => ::1
350-
if (hostHeader.startsWith('[')) {
350+
if (hostHeader[0] === '[') {
351351
const index = hostHeader.indexOf(']');
352352
if (index === -1) {
353353
// Leading '[', but no ']'. Need to do something...

‎lib/internal/main/watch_mode.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ for (let i = 0; i < process.execArgv.length; i++) {
4545
if (StringPrototypeStartsWith(arg, '--watch')) {
4646
i++;
4747
const nextArg = process.execArgv[i];
48-
if (nextArg && StringPrototypeStartsWith(nextArg, '-')) {
48+
if (nextArg && nextArg[0] === '-') {
4949
ArrayPrototypePush(argsWithoutWatchOptions, nextArg);
5050
}
5151
continue;

‎lib/internal/modules/esm/fetch_module.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@ const {
33
ObjectPrototypeHasOwnProperty,
44
PromisePrototypeThen,
55
SafeMap,
6-
StringPrototypeEndsWith,
76
StringPrototypeSlice,
8-
StringPrototypeStartsWith,
97
} = primordials;
108
const {
119
Buffer: { concat: BufferConcat },
@@ -248,8 +246,9 @@ allowList.addRange('127.0.0.1', '127.255.255.255');
248246
async function isLocalAddress(hostname) {
249247
try {
250248
if (
251-
StringPrototypeStartsWith(hostname, '[') &&
252-
StringPrototypeEndsWith(hostname, ']')
249+
hostname.length &&
250+
hostname[0] === '[' &&
251+
hostname[hostname.length - 1] === ']'
253252
) {
254253
hostname = StringPrototypeSlice(hostname, 1, -1);
255254
}

‎lib/internal/modules/esm/resolve.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -399,8 +399,9 @@ function resolvePackageTargetString(
399399
}
400400

401401
if (!StringPrototypeStartsWith(target, './')) {
402-
if (internal && !StringPrototypeStartsWith(target, '../') &&
403-
!StringPrototypeStartsWith(target, '/')) {
402+
if (internal &&
403+
target[0] !== '/' &&
404+
!StringPrototypeStartsWith(target, '../')) {
404405
// No need to convert target to string, since it's already presumed to be
405406
if (!URLCanParse(target)) {
406407
const exportTarget = pattern ?

‎lib/internal/modules/helpers.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ function addBuiltinLibsToObject(object, dummyModuleName) {
244244
ArrayPrototypeForEach(builtinModules, (name) => {
245245
// Neither add underscored modules, nor ones that contain slashes (e.g.,
246246
// 'fs/promises') or ones that are already defined.
247-
if (StringPrototypeStartsWith(name, '_') ||
247+
if (name[0] === '_' ||
248248
StringPrototypeIncludes(name, '/') ||
249249
ObjectPrototypeHasOwnProperty(object, name)) {
250250
return;

‎lib/internal/process/per_thread.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ const {
2525
StringPrototypeEndsWith,
2626
StringPrototypeReplace,
2727
StringPrototypeSlice,
28-
StringPrototypeStartsWith,
2928
Symbol,
3029
SymbolIterator,
3130
} = primordials;
@@ -302,7 +301,7 @@ function buildAllowedFlags() {
302301
}
303302

304303
function isAccepted(to) {
305-
if (!StringPrototypeStartsWith(to, '-') || to === '--') return true;
304+
if (!to.length || to[0] !== '-' || to === '--') return true;
306305
const recursiveExpansion = aliases.get(to);
307306
if (recursiveExpansion) {
308307
if (recursiveExpansion[0] === to)

‎lib/internal/process/pre_execution.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ const {
1515
ObjectGetOwnPropertyDescriptor,
1616
SafeMap,
1717
String,
18-
StringPrototypeStartsWith,
1918
Symbol,
2019
globalThis,
2120
} = primordials;
@@ -244,8 +243,7 @@ function patchProcessObject(expandArgv1) {
244243
let mainEntry;
245244
// If requested, update process.argv[1] to replace whatever the user provided with the resolved absolute file path of
246245
// the entry point.
247-
if (expandArgv1 && process.argv[1] &&
248-
!StringPrototypeStartsWith(process.argv[1], '-')) {
246+
if (expandArgv1 && process.argv[1] && process.argv[1][0] !== '-') {
249247
// Expand process.argv[1] into a full path.
250248
const path = require('path');
251249
try {

‎lib/internal/repl/utils.js

+1-4
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,10 @@ const {
1010
RegExpPrototypeExec,
1111
SafeSet,
1212
SafeStringIterator,
13-
StringPrototypeEndsWith,
1413
StringPrototypeIndexOf,
1514
StringPrototypeLastIndexOf,
1615
StringPrototypeReplaceAll,
1716
StringPrototypeSlice,
18-
StringPrototypeStartsWith,
1917
StringPrototypeToLowerCase,
2018
StringPrototypeTrim,
2119
Symbol,
@@ -298,8 +296,7 @@ function setupPreview(repl, contextSymbol, bufferSymbol, active) {
298296
function getInputPreview(input, callback) {
299297
// For similar reasons as `defaultEval`, wrap expressions starting with a
300298
// curly brace with parenthesis.
301-
if (StringPrototypeStartsWith(input, '{') &&
302-
!StringPrototypeEndsWith(input, ';') && !wrapped) {
299+
if (!wrapped && input[0] === '{' && input[input.length - 1] !== ';') {
303300
input = `(${input})`;
304301
wrapped = true;
305302
}

‎lib/internal/url.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1408,7 +1408,7 @@ function urlToHttpOptions(url) {
14081408
__proto__: null,
14091409
...url, // In case the url object was extended by the user.
14101410
protocol: url.protocol,
1411-
hostname: hostname && StringPrototypeStartsWith(hostname, '[') ?
1411+
hostname: hostname && hostname[0] === '[' ?
14121412
StringPrototypeSlice(hostname, 1, -1) :
14131413
hostname,
14141414
hash: url.hash,

‎lib/internal/util/inspect.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -1181,7 +1181,7 @@ function getClassBase(value, constructor, tag) {
11811181

11821182
function getFunctionBase(value, constructor, tag) {
11831183
const stringified = FunctionPrototypeToString(value);
1184-
if (StringPrototypeStartsWith(stringified, 'class') && StringPrototypeEndsWith(stringified, '}')) {
1184+
if (StringPrototypeStartsWith(stringified, 'class') && stringified[stringified.length - 1] === '}') {
11851185
const slice = StringPrototypeSlice(stringified, 5, -1);
11861186
const bracketIndex = StringPrototypeIndexOf(slice, '{');
11871187
if (bracketIndex !== -1 &&
@@ -1560,7 +1560,8 @@ function handleMaxCallStackSize(ctx, err, constructorName, indentationLvl) {
15601560
function addNumericSeparator(integerString) {
15611561
let result = '';
15621562
let i = integerString.length;
1563-
const start = StringPrototypeStartsWith(integerString, '-') ? 1 : 0;
1563+
assert(i !== 0);
1564+
const start = integerString[0] === '-' ? 1 : 0;
15641565
for (; i >= start + 4; i -= 3) {
15651566
result = `_${StringPrototypeSlice(integerString, i - 3, i)}${result}`;
15661567
}

‎lib/repl.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ const { shouldColorize } = require('internal/util/colors');
130130
const CJSModule = require('internal/modules/cjs/loader').Module;
131131
let _builtinLibs = ArrayPrototypeFilter(
132132
CJSModule.builtinModules,
133-
(e) => !StringPrototypeStartsWith(e, '_'),
133+
(e) => e[0] !== '_',
134134
);
135135
const nodeSchemeBuiltinLibs = ArrayPrototypeMap(
136136
_builtinLibs, (lib) => `node:${lib}`);

0 commit comments

Comments
 (0)