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.

rulesfunction-declarations.md

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

Function Declarations vs Arrow Functions

Prefer function declarations for named functions. Use arrow functions for inline callbacks or when needed for type inference.

Prefer Function Declarations

// Good: function declaration
function calculateTotal(items: Item[]): number {
  let total = 0;
  for (let item of items) {
    total += item.price * item.quantity;
  }
  return total;
}

function formatUser(user: User): string {
  return `${user.firstName} ${user.lastName}`;
}

async function fetchUsers(): Promise<User[]> {
  let response = await fetch("/api/users");
  return response.json();
}

// Avoid: arrow function for named functions
const calculateTotal = (items: Item[]): number => {
  let total = 0;
  for (let item of items) {
    total += item.price * item.quantity;
  }
  return total;
};

Use Arrow Functions For

1. Inline Callbacks
// Good: arrow for inline callbacks
let activeUsers = users.filter((user) => user.isActive);
let names = users.map((user) => user.name);
let sorted = items.toSorted((a, b) => a.price - b.price);

// Avoid: function expression for simple callbacks
let activeUsers = users.filter(function (user) {
  return user.isActive;
});
2. When Types Require Arrow Syntax
// Good: arrow needed for proper typing
const createHandler: ActionFunction = async ({ request }) => {
  let formData = await request.formData();
  // ...
};

// Good: arrow for typed event handlers
const handleClick: MouseEventHandler<HTMLButtonElement> = (event) => {
  event.preventDefault();
  // ...
};

// Good: arrow for React component props
const renderItem: ListRenderItem<User> = ({ item }) => (
  <UserCard user={item} />
);

Why Function Declarations

  1. Hoisting - Can be called before definition (useful for organizing code)
  2. Better stack traces - Named functions show up clearly in error stacks
  3. Clearer intent - function keyword signals "this is a function"
  4. Consistency - One style for all named functions

Summary

Use Case Syntax
Named functions (including short utilities) function name() {}
Inline callbacks (x) => x.value
Typed handlers/callbacks const handler: Type = () => {}

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