Unverified Commit 42370cf3 authored by Luke Sandberg's avatar Luke Sandberg Committed by GitHub

[turbopack] replace regex with indexof (#92023)

Replace regex-based `isJs`/`isCss` checks with string operations (`indexOf` + `startsWith`) to avoid regex overhead on a hot path. Extracts a shared `endsWithExtension` helper that finds the end of the path portion (before `?` or `#`) and checks the extension without allocating substrings.
parent feb594bd
...@@ -406,20 +406,33 @@ function getChunkFromRegistration( ...@@ -406,20 +406,33 @@ function getChunkFromRegistration(
} }
} }
const regexJsUrl = /\.js(?:\?[^#]*)?(?:#.*)?$/
/** /**
* Checks if a given path/URL ends with .js, optionally followed by ?query or #fragment. * Checks if a given path/URL ends with the given extension,
* optionally followed by ?query or #fragment.
*/ */
function endsWithExtension(
chunkUrlOrPath: ChunkUrl | ChunkPath,
ext: string
): boolean {
// Find where the path ends (before query or fragment)
const q = chunkUrlOrPath.indexOf('?')
let end: number
if (q !== -1) {
end = q
} else {
const h = chunkUrlOrPath.indexOf('#')
end = h !== -1 ? h : chunkUrlOrPath.length
}
// Check if the path portion ends with the extension
return end >= ext.length && chunkUrlOrPath.startsWith(ext, end - ext.length)
}
function isJs(chunkUrlOrPath: ChunkUrl | ChunkPath): boolean { function isJs(chunkUrlOrPath: ChunkUrl | ChunkPath): boolean {
return regexJsUrl.test(chunkUrlOrPath) return endsWithExtension(chunkUrlOrPath, '.js')
} }
const regexCssUrl = /\.css(?:\?[^#]*)?(?:#.*)?$/
/**
* Checks if a given path/URL ends with .css, optionally followed by ?query or #fragment.
*/
function isCss(chunkUrl: ChunkUrl): boolean { function isCss(chunkUrl: ChunkUrl): boolean {
return regexCssUrl.test(chunkUrl) return endsWithExtension(chunkUrl, '.css')
} }
function loadWebAssembly( function loadWebAssembly(
......
;!function(){try { var e="undefined"!=typeof globalThis?globalThis:"undefined"!=typeof global?global:"undefined"!=typeof window?window:"undefined"!=typeof self?self:{},n=(new e.Error).stack;n&&((e._debugIds|| (e._debugIds={}))[n]="a3e3071c-9f3a-ff6e-9007-f87d45699783")}catch(e){}}(); ;!function(){try { var e="undefined"!=typeof globalThis?globalThis:"undefined"!=typeof global?global:"undefined"!=typeof window?window:"undefined"!=typeof self?self:{},n=(new e.Error).stack;n&&((e._debugIds|| (e._debugIds={}))[n]="161bf1fe-ca33-d8de-b14a-ddace1bb63c7")}catch(e){}}();
(globalThis["TURBOPACK"] || (globalThis["TURBOPACK"] = [])).push([ (globalThis["TURBOPACK"] || (globalThis["TURBOPACK"] = [])).push([
"output/1i9t_crates_turbopack-tests_tests_snapshot_debug-ids_browser_input_index_19boa0e.js", "output/1i9t_crates_turbopack-tests_tests_snapshot_debug-ids_browser_input_index_19boa0e.js",
{"otherChunks":["output/1do3_crates_turbopack-tests_tests_snapshot_debug-ids_browser_input_index_03ibyvs.js"],"runtimeModuleIds":["[project]/turbopack/crates/turbopack-tests/tests/snapshot/debug-ids/browser/input/index.js [test] (ecmascript)"]} {"otherChunks":["output/1do3_crates_turbopack-tests_tests_snapshot_debug-ids_browser_input_index_03ibyvs.js"],"runtimeModuleIds":["[project]/turbopack/crates/turbopack-tests/tests/snapshot/debug-ids/browser/input/index.js [test] (ecmascript)"]}
...@@ -818,17 +818,27 @@ function getPathFromScript(chunkScript) { ...@@ -818,17 +818,27 @@ function getPathFromScript(chunkScript) {
}; };
} }
} }
const regexJsUrl = /\.js(?:\?[^#]*)?(?:#.*)?$/;
/** /**
* Checks if a given path/URL ends with .js, optionally followed by ?query or #fragment. * Checks if a given path/URL ends with the given extension,
*/ function isJs(chunkUrlOrPath) { * optionally followed by ?query or #fragment.
return regexJsUrl.test(chunkUrlOrPath); */ function endsWithExtension(chunkUrlOrPath, ext) {
// Find where the path ends (before query or fragment)
const q = chunkUrlOrPath.indexOf('?');
let end;
if (q !== -1) {
end = q;
} else {
const h = chunkUrlOrPath.indexOf('#');
end = h !== -1 ? h : chunkUrlOrPath.length;
}
// Check if the path portion ends with the extension
return end >= ext.length && chunkUrlOrPath.startsWith(ext, end - ext.length);
} }
const regexCssUrl = /\.css(?:\?[^#]*)?(?:#.*)?$/; function isJs(chunkUrlOrPath) {
/** return endsWithExtension(chunkUrlOrPath, '.js');
* Checks if a given path/URL ends with .css, optionally followed by ?query or #fragment. }
*/ function isCss(chunkUrl) { function isCss(chunkUrl) {
return regexCssUrl.test(chunkUrl); return endsWithExtension(chunkUrl, '.css');
} }
function loadWebAssembly(chunkPath, edgeModule, importsObj) { function loadWebAssembly(chunkPath, edgeModule, importsObj) {
return BACKEND.loadWebAssembly(SourceType.Parent, this.m.id, chunkPath, edgeModule, importsObj); return BACKEND.loadWebAssembly(SourceType.Parent, this.m.id, chunkPath, edgeModule, importsObj);
...@@ -2217,5 +2227,5 @@ chunkListsToRegister.forEach(registerChunkList); ...@@ -2217,5 +2227,5 @@ chunkListsToRegister.forEach(registerChunkList);
})(); })();
//# debugId=a3e3071c-9f3a-ff6e-9007-f87d45699783 //# debugId=161bf1fe-ca33-d8de-b14a-ddace1bb63c7
//# sourceMappingURL=1do3_crates_turbopack-tests_tests_snapshot_debug-ids_browser_input_index_19boa0e.js.map //# sourceMappingURL=1do3_crates_turbopack-tests_tests_snapshot_debug-ids_browser_input_index_19boa0e.js.map
\ No newline at end of file
...@@ -817,17 +817,27 @@ function getPathFromScript(chunkScript) { ...@@ -817,17 +817,27 @@ function getPathFromScript(chunkScript) {
}; };
} }
} }
const regexJsUrl = /\.js(?:\?[^#]*)?(?:#.*)?$/;
/** /**
* Checks if a given path/URL ends with .js, optionally followed by ?query or #fragment. * Checks if a given path/URL ends with the given extension,
*/ function isJs(chunkUrlOrPath) { * optionally followed by ?query or #fragment.
return regexJsUrl.test(chunkUrlOrPath); */ function endsWithExtension(chunkUrlOrPath, ext) {
// Find where the path ends (before query or fragment)
const q = chunkUrlOrPath.indexOf('?');
let end;
if (q !== -1) {
end = q;
} else {
const h = chunkUrlOrPath.indexOf('#');
end = h !== -1 ? h : chunkUrlOrPath.length;
}
// Check if the path portion ends with the extension
return end >= ext.length && chunkUrlOrPath.startsWith(ext, end - ext.length);
} }
const regexCssUrl = /\.css(?:\?[^#]*)?(?:#.*)?$/; function isJs(chunkUrlOrPath) {
/** return endsWithExtension(chunkUrlOrPath, '.js');
* Checks if a given path/URL ends with .css, optionally followed by ?query or #fragment. }
*/ function isCss(chunkUrl) { function isCss(chunkUrl) {
return regexCssUrl.test(chunkUrl); return endsWithExtension(chunkUrl, '.css');
} }
function loadWebAssembly(chunkPath, edgeModule, importsObj) { function loadWebAssembly(chunkPath, edgeModule, importsObj) {
return BACKEND.loadWebAssembly(SourceType.Parent, this.m.id, chunkPath, edgeModule, importsObj); return BACKEND.loadWebAssembly(SourceType.Parent, this.m.id, chunkPath, edgeModule, importsObj);
......
...@@ -1387,17 +1387,27 @@ function getPathFromScript(chunkScript) { ...@@ -1387,17 +1387,27 @@ function getPathFromScript(chunkScript) {
}; };
} }
} }
var regexJsUrl = /\.js(?:\?[^#]*)?(?:#.*)?$/;
/** /**
* Checks if a given path/URL ends with .js, optionally followed by ?query or #fragment. * Checks if a given path/URL ends with the given extension,
*/ function isJs(chunkUrlOrPath) { * optionally followed by ?query or #fragment.
return regexJsUrl.test(chunkUrlOrPath); */ function endsWithExtension(chunkUrlOrPath, ext) {
// Find where the path ends (before query or fragment)
var q = chunkUrlOrPath.indexOf('?');
var end;
if (q !== -1) {
end = q;
} else {
var h = chunkUrlOrPath.indexOf('#');
end = h !== -1 ? h : chunkUrlOrPath.length;
}
// Check if the path portion ends with the extension
return end >= ext.length && chunkUrlOrPath.startsWith(ext, end - ext.length);
} }
var regexCssUrl = /\.css(?:\?[^#]*)?(?:#.*)?$/; function isJs(chunkUrlOrPath) {
/** return endsWithExtension(chunkUrlOrPath, '.js');
* Checks if a given path/URL ends with .css, optionally followed by ?query or #fragment. }
*/ function isCss(chunkUrl) { function isCss(chunkUrl) {
return regexCssUrl.test(chunkUrl); return endsWithExtension(chunkUrl, '.css');
} }
function loadWebAssembly(chunkPath, edgeModule, importsObj) { function loadWebAssembly(chunkPath, edgeModule, importsObj) {
return BACKEND.loadWebAssembly(SourceType.Parent, this.m.id, chunkPath, edgeModule, importsObj); return BACKEND.loadWebAssembly(SourceType.Parent, this.m.id, chunkPath, edgeModule, importsObj);
......
...@@ -817,17 +817,27 @@ function getPathFromScript(chunkScript) { ...@@ -817,17 +817,27 @@ function getPathFromScript(chunkScript) {
}; };
} }
} }
const regexJsUrl = /\.js(?:\?[^#]*)?(?:#.*)?$/;
/** /**
* Checks if a given path/URL ends with .js, optionally followed by ?query or #fragment. * Checks if a given path/URL ends with the given extension,
*/ function isJs(chunkUrlOrPath) { * optionally followed by ?query or #fragment.
return regexJsUrl.test(chunkUrlOrPath); */ function endsWithExtension(chunkUrlOrPath, ext) {
// Find where the path ends (before query or fragment)
const q = chunkUrlOrPath.indexOf('?');
let end;
if (q !== -1) {
end = q;
} else {
const h = chunkUrlOrPath.indexOf('#');
end = h !== -1 ? h : chunkUrlOrPath.length;
}
// Check if the path portion ends with the extension
return end >= ext.length && chunkUrlOrPath.startsWith(ext, end - ext.length);
} }
const regexCssUrl = /\.css(?:\?[^#]*)?(?:#.*)?$/; function isJs(chunkUrlOrPath) {
/** return endsWithExtension(chunkUrlOrPath, '.js');
* Checks if a given path/URL ends with .css, optionally followed by ?query or #fragment. }
*/ function isCss(chunkUrl) { function isCss(chunkUrl) {
return regexCssUrl.test(chunkUrl); return endsWithExtension(chunkUrl, '.css');
} }
function loadWebAssembly(chunkPath, edgeModule, importsObj) { function loadWebAssembly(chunkPath, edgeModule, importsObj) {
return BACKEND.loadWebAssembly(SourceType.Parent, this.m.id, chunkPath, edgeModule, importsObj); return BACKEND.loadWebAssembly(SourceType.Parent, this.m.id, chunkPath, edgeModule, importsObj);
......
...@@ -817,17 +817,27 @@ function getPathFromScript(chunkScript) { ...@@ -817,17 +817,27 @@ function getPathFromScript(chunkScript) {
}; };
} }
} }
const regexJsUrl = /\.js(?:\?[^#]*)?(?:#.*)?$/;
/** /**
* Checks if a given path/URL ends with .js, optionally followed by ?query or #fragment. * Checks if a given path/URL ends with the given extension,
*/ function isJs(chunkUrlOrPath) { * optionally followed by ?query or #fragment.
return regexJsUrl.test(chunkUrlOrPath); */ function endsWithExtension(chunkUrlOrPath, ext) {
// Find where the path ends (before query or fragment)
const q = chunkUrlOrPath.indexOf('?');
let end;
if (q !== -1) {
end = q;
} else {
const h = chunkUrlOrPath.indexOf('#');
end = h !== -1 ? h : chunkUrlOrPath.length;
}
// Check if the path portion ends with the extension
return end >= ext.length && chunkUrlOrPath.startsWith(ext, end - ext.length);
} }
const regexCssUrl = /\.css(?:\?[^#]*)?(?:#.*)?$/; function isJs(chunkUrlOrPath) {
/** return endsWithExtension(chunkUrlOrPath, '.js');
* Checks if a given path/URL ends with .css, optionally followed by ?query or #fragment. }
*/ function isCss(chunkUrl) { function isCss(chunkUrl) {
return regexCssUrl.test(chunkUrl); return endsWithExtension(chunkUrl, '.css');
} }
function loadWebAssembly(chunkPath, edgeModule, importsObj) { function loadWebAssembly(chunkPath, edgeModule, importsObj) {
return BACKEND.loadWebAssembly(SourceType.Parent, this.m.id, chunkPath, edgeModule, importsObj); return BACKEND.loadWebAssembly(SourceType.Parent, this.m.id, chunkPath, edgeModule, importsObj);
......
...@@ -817,17 +817,27 @@ function getPathFromScript(chunkScript) { ...@@ -817,17 +817,27 @@ function getPathFromScript(chunkScript) {
}; };
} }
} }
const regexJsUrl = /\.js(?:\?[^#]*)?(?:#.*)?$/;
/** /**
* Checks if a given path/URL ends with .js, optionally followed by ?query or #fragment. * Checks if a given path/URL ends with the given extension,
*/ function isJs(chunkUrlOrPath) { * optionally followed by ?query or #fragment.
return regexJsUrl.test(chunkUrlOrPath); */ function endsWithExtension(chunkUrlOrPath, ext) {
// Find where the path ends (before query or fragment)
const q = chunkUrlOrPath.indexOf('?');
let end;
if (q !== -1) {
end = q;
} else {
const h = chunkUrlOrPath.indexOf('#');
end = h !== -1 ? h : chunkUrlOrPath.length;
}
// Check if the path portion ends with the extension
return end >= ext.length && chunkUrlOrPath.startsWith(ext, end - ext.length);
} }
const regexCssUrl = /\.css(?:\?[^#]*)?(?:#.*)?$/; function isJs(chunkUrlOrPath) {
/** return endsWithExtension(chunkUrlOrPath, '.js');
* Checks if a given path/URL ends with .css, optionally followed by ?query or #fragment. }
*/ function isCss(chunkUrl) { function isCss(chunkUrl) {
return regexCssUrl.test(chunkUrl); return endsWithExtension(chunkUrl, '.css');
} }
function loadWebAssembly(chunkPath, edgeModule, importsObj) { function loadWebAssembly(chunkPath, edgeModule, importsObj) {
return BACKEND.loadWebAssembly(SourceType.Parent, this.m.id, chunkPath, edgeModule, importsObj); return BACKEND.loadWebAssembly(SourceType.Parent, this.m.id, chunkPath, edgeModule, importsObj);
......
...@@ -817,17 +817,27 @@ function getPathFromScript(chunkScript) { ...@@ -817,17 +817,27 @@ function getPathFromScript(chunkScript) {
}; };
} }
} }
const regexJsUrl = /\.js(?:\?[^#]*)?(?:#.*)?$/;
/** /**
* Checks if a given path/URL ends with .js, optionally followed by ?query or #fragment. * Checks if a given path/URL ends with the given extension,
*/ function isJs(chunkUrlOrPath) { * optionally followed by ?query or #fragment.
return regexJsUrl.test(chunkUrlOrPath); */ function endsWithExtension(chunkUrlOrPath, ext) {
// Find where the path ends (before query or fragment)
const q = chunkUrlOrPath.indexOf('?');
let end;
if (q !== -1) {
end = q;
} else {
const h = chunkUrlOrPath.indexOf('#');
end = h !== -1 ? h : chunkUrlOrPath.length;
}
// Check if the path portion ends with the extension
return end >= ext.length && chunkUrlOrPath.startsWith(ext, end - ext.length);
} }
const regexCssUrl = /\.css(?:\?[^#]*)?(?:#.*)?$/; function isJs(chunkUrlOrPath) {
/** return endsWithExtension(chunkUrlOrPath, '.js');
* Checks if a given path/URL ends with .css, optionally followed by ?query or #fragment. }
*/ function isCss(chunkUrl) { function isCss(chunkUrl) {
return regexCssUrl.test(chunkUrl); return endsWithExtension(chunkUrl, '.css');
} }
function loadWebAssembly(chunkPath, edgeModule, importsObj) { function loadWebAssembly(chunkPath, edgeModule, importsObj) {
return BACKEND.loadWebAssembly(SourceType.Parent, this.m.id, chunkPath, edgeModule, importsObj); return BACKEND.loadWebAssembly(SourceType.Parent, this.m.id, chunkPath, edgeModule, importsObj);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment