Skip to content

Commit dc38d86

Browse files
anpeteclaude
andcommitted
refactor(page-context): collapse JSON-LD type collection onto a single Set
Address @jonathanKingston review: a Set already dedupes and preserves insertion order, so it replaces the parallel types array + seen Set. Cap via .size, drop the now-redundant slice (recordType short-circuits at the cap), and rename add -> recordType. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent f286b08 commit dc38d86

1 file changed

Lines changed: 14 additions & 17 deletions

File tree

injected/src/features/page-context.js

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -248,25 +248,22 @@ export function extractPageTypeSignals(document, { maxTypes = 10, maxBlockLength
248248
* @returns {string[]}
249249
*/
250250
function extractJsonLdTypes(document, maxTypes, maxBlockLength) {
251-
/** @type {string[]} */
252-
const types = [];
253-
const seen = new Set();
251+
// A Set both dedupes and preserves insertion order, so it doubles as the store and the cap counter.
252+
const types = new Set();
254253
/** @param {unknown} value */
255-
const add = (value) => {
254+
const recordType = (value) => {
256255
// Short-circuit once the cap is hit so a single wide `@graph` doesn't keep iterating.
257-
if (types.length >= maxTypes) return;
256+
if (types.size >= maxTypes) return;
258257
if (typeof value !== 'string') return;
259258
const type = value.trim();
260-
if (!type || seen.has(type)) return;
261-
seen.add(type);
262-
types.push(type);
259+
if (type) types.add(type);
263260
};
264261

265262
// Case-insensitive attribute match ("i") so non-canonical casing (e.g. APPLICATION/LD+JSON)
266263
// is still discovered.
267264
const blocks = document.querySelectorAll('script[type="application/ld+json" i]');
268265
for (const block of blocks) {
269-
if (types.length >= maxTypes) break;
266+
if (types.size >= maxTypes) break;
270267
const text = block.textContent || '';
271268
if (!text || text.length > maxBlockLength) continue;
272269
let data;
@@ -275,38 +272,38 @@ function extractJsonLdTypes(document, maxTypes, maxBlockLength) {
275272
} catch (e) {
276273
continue;
277274
}
278-
collectJsonLdTypes(data, add);
275+
collectJsonLdTypes(data, recordType);
279276
}
280277

281-
return types.slice(0, maxTypes);
278+
return Array.from(types);
282279
}
283280

284281
/**
285282
* Walk a parsed JSON-LD value (object, array, or `@graph` container) and report each `@type`.
286283
* Own-property checks keep a polluted `Object.prototype` from surfacing spurious `@type` /
287284
* `@graph` keys, and recursion is bounded by {@link MAX_JSON_LD_DEPTH}.
288285
* @param {unknown} node
289-
* @param {(value: unknown) => void} add
286+
* @param {(value: unknown) => void} recordType
290287
* @param {number} [depth]
291288
*/
292-
function collectJsonLdTypes(node, add, depth = 0) {
289+
function collectJsonLdTypes(node, recordType, depth = 0) {
293290
if (depth > MAX_JSON_LD_DEPTH) return;
294291
if (Array.isArray(node)) {
295-
for (const item of node) collectJsonLdTypes(item, add, depth + 1);
292+
for (const item of node) collectJsonLdTypes(item, recordType, depth + 1);
296293
return;
297294
}
298295
if (!node || typeof node !== 'object') return;
299296
const obj = /** @type {Record<string, unknown>} */ (node);
300297
if (hasOwnProperty.call(obj, '@type')) {
301298
const type = obj['@type'];
302299
if (Array.isArray(type)) {
303-
for (const value of type) add(value);
300+
for (const value of type) recordType(value);
304301
} else if (type != null) {
305-
add(type);
302+
recordType(type);
306303
}
307304
}
308305
if (hasOwnProperty.call(obj, '@graph')) {
309-
collectJsonLdTypes(obj['@graph'], add, depth + 1);
306+
collectJsonLdTypes(obj['@graph'], recordType, depth + 1);
310307
}
311308
}
312309

0 commit comments

Comments
 (0)