Skip to content
This repository was archived by the owner on Sep 21, 2021. It is now read-only.

Commit bb31728

Browse files
committed
Speed up containsURL function.
The function was splitting up a string and testing if each parts was an actual URL, until one was encountered. The split function was showing up in some profile I did today (nothing too big, but bigger that it should be). Here switching to a simple regex test works and is faster to run.
1 parent e574458 commit bb31728

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

packages/devtools-reps/src/reps/rep-utils.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
44

55
// Dependencies
6-
const validProtocols = /^(http|https|ftp|data|resource|chrome):/i;
6+
const validProtocols = /(http|https|ftp|data|resource|chrome):/i;
77
const tokenSplitRegex = /(\s|\'|\"|\\)+/;
88
const ELLIPSIS = "\u2026";
99
const dom = require("react-dom-factories");
@@ -359,12 +359,12 @@ function getGripType(object, noGrip) {
359359
* Whether the grip is a string containing a URL.
360360
*/
361361
function containsURL(grip) {
362-
if (typeof grip !== "string") {
362+
// An URL can't be shorter than 5 char (e.g. "ftp:").
363+
if (typeof grip !== "string" || grip.length < 5) {
363364
return false;
364365
}
365366

366-
let tokens = grip.split(tokenSplitRegex);
367-
return tokens.some(isURL);
367+
return validProtocols.test(grip);
368368
}
369369

370370
/**

0 commit comments

Comments
 (0)