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.

rulescache-function-results.md

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

Cache Repeated Function Calls

Use a module-level Map to cache function results when the same function is called repeatedly with the same inputs.

Incorrect (redundant computation):

function processProjects(projects: Project[]) {
  return projects.map((project) => {
    // slugify() called 100+ times for same project names
    let slug = slugify(project.name);
    return { ...project, slug };
  });
}

Correct (cached results):

// Module-level cache
const slugifyCache = new Map<string, string>();

function cachedSlugify(text: string): string {
  if (slugifyCache.has(text)) {
    return slugifyCache.get(text)!;
  }
  let result = slugify(text);
  slugifyCache.set(text, result);
  return result;
}

function processProjects(projects: Project[]) {
  return projects.map((project) => {
    // Computed only once per unique project name
    let slug = cachedSlugify(project.name);
    return { ...project, slug };
  });
}

Simpler pattern for single-value functions:

let isLoggedInCache: boolean | null = null;

function isLoggedIn(): boolean {
  if (isLoggedInCache !== null) {
    return isLoggedInCache;
  }

  isLoggedInCache = document.cookie.includes("auth=");
  return isLoggedInCache;
}

// Clear cache when auth changes
function onAuthChange() {
  isLoggedInCache = null;
}

With LRU limits (prevent memory leaks):

const MAX_CACHE_SIZE = 1000;

function cachedSlugify(text: string): string {
  if (slugifyCache.has(text)) {
    return slugifyCache.get(text)!;
  }

  // Prevent unbounded growth
  if (slugifyCache.size >= MAX_CACHE_SIZE) {
    let firstKey = slugifyCache.keys().next().value;
    slugifyCache.delete(firstKey);
  }

  let result = slugify(text);
  slugifyCache.set(text, result);
  return result;
}

Use a Map (not React hooks) so it works everywhere: utilities, event handlers, loaders, etc.

Source: SKILL.md on GitHub

1 warning13d5 checks · Risk SAFE
  • Gen Agent Trust Hub13d

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

  • Socket13d

    No alerts

  • Snyk13d

    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