All skills
sergiodxa avatar

/frontend-js-best-practices

@2987820

JavaScript performance optimization guidelines. Use when writing, reviewing, or refactoring JavaScript/TypeScript code to ensure optimal performance patterns. Triggers on tasks involving loops, data structures, DOM manipulation, or general JS optimization.

Use this Skill: https://skilld.dev/gh/sergiodxa/agent-skills/frontend-js-best-practices

This session only. Nothing lands on disk.

ruleshoist-regexp.md

≈401 tokens on demand. Your agent reads this file only when SKILL.md points to it.

Hoist RegExp Creation

Don't create RegExp inside loops or frequently-called functions. Hoist to module scope or cache the result.

Incorrect (new RegExp every call):

function highlightMatches(text: string, query: string) {
  let regex = new RegExp(`(${query})`, "gi"); // Created every call
  return text.split(regex);
}

// In a loop - creates regex 1000 times
items.forEach((item) => {
  let matches = highlightMatches(item.text, searchQuery);
});

Correct (hoist static patterns):

const EMAIL_REGEX = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
const URL_REGEX = /https?:\/\/[^\s]+/g;

function isValidEmail(email: string) {
  return EMAIL_REGEX.test(email);
}

Correct (cache dynamic patterns):

const regexCache = new Map<string, RegExp>();

function getHighlightRegex(query: string): RegExp {
  if (!regexCache.has(query)) {
    regexCache.set(query, new RegExp(`(${escapeRegex(query)})`, "gi"));
  }
  return regexCache.get(query)!;
}

function highlightMatches(text: string, query: string) {
  let regex = getHighlightRegex(query);
  return text.split(regex);
}

Warning (global regex has mutable state):

Global regex (/g) has mutable lastIndex state:

const regex = /foo/g;
regex.test("foo"); // true, lastIndex = 3
regex.test("foo"); // false, lastIndex = 0 (unexpected!)

Reset lastIndex before reuse, or create a new regex for each use when using /g flag.

Source: SKILL.md on GitHub

1 warning14d5 checks · Risk SAFE
  • Gen Agent Trust Hub14d

    The skill provides a comprehensive set of JavaScript and TypeScript performance and style guidelines. No security issues were detected.

  • Socket14d

    No alerts

  • Snyk14d

    Risk: LOW · No issues

  • Runlayer7mo

    19/19 files flagged

  • ZeroLeaks5mo

    Score: 93/100 · 2 sections analyzed

Signed by skilld at 2987820. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub 2 months ago.

Dormantupdated 8 months ago

README badge

README badge for sergiodxa/agent-skills/frontend-js-best-practices