modelscope.cn

javascript

JavaScript micro-optimizations and performance patterns. Use when optimizing loops, array operations, caching, or DOM manipulation. Includes Set/Map usage, early returns, and memory-efficient patterns.

Installation

$ npx skills add https://modelscope.cn

Also in this package

Other skills from modelscope.cn · top by installs.

npx skills add https://modelscope.cn

Browse all from modelscope.cn

More details

Agent compatibility

Declared targets from SKILL.md / docs. Unmarked agents are not listed — the skill may still install via the CLI.

Claude Code Not declared
Cursor Not declared
Codex Not declared
GitHub Copilot Not declared
Windsurf Not declared
Gemini CLI Not declared
Cline Not declared
OpenCode Not declared

Package contents

Files included with this skill beyond the listing page.

  • skill md SKILL.md 2,808 B

History

  1. First recorded snapshot · 0 installs

SKILL.md

JavaScript Performance

Micro-optimizations for high-performance JavaScript.

Instructions

1. Use Set for Lookups

// ❌ Bad - O(n) lookup
const ids = [1, 2, 3, 4, 5];
if (ids.includes(targetId)) { ... }

// ✅ Good - O(1) lookup
const idSet = new Set([1, 2, 3, 4, 5]);
if (idSet.has(targetId)) { ... }

2. Use Map for Key-Value

// ❌ Bad - object for dynamic keys
const cache: { [key: string]: Data } = {};
cache[key] = data;

// ✅ Good - Map for dynamic keys
const cache = new Map<string, Data>();
cache.set(key, data);

3. Early Returns

// ❌ Bad - nested conditions
function process(data) {
  if (data) {
    if (data.isValid) {
      if (data.type === 'user') {
        return handleUser(data);
      }
    }
  }
  return null;
}

// ✅ Good - early returns
function process(data) {
  if (!data) return null;
  if (!data.isValid) return null;
  if (data.type !== 'user') return null;
  return handleUser(data);
}

4. Avoid Creating Arrays

// ❌ Bad - creates intermediate array
const result = items.filter(x => x.active).map(x => x.name)[0];

// ✅ Good - find first match
const result = items.find(x => x.active)?.name;

5. Batch DOM Updates

// ❌ Bad - multiple reflows
items.forEach(item => {
  const el = document.createElement('div');
  el.textContent = item.name;
  container.appendChild(el);
});

// ✅ Good - single reflow
const fragment = document.createDocumentFragment();
items.forEach(item => {
  const el = document.createElement('div');
  el.textContent = item.name;
  fragment.appendChild(el);
});
container.appendChild(fragment);

6. Passive Event Listeners

// ✅ Use passive for scroll/touch events
element.addEventListener('scroll', handler, { passive: true });
element.addEventListener('touchstart', handler, { passive: true });

7. Caching Expensive Operations

// ✅ Memoization
const cache = new Map();

function expensiveCalculation(input: string) {
  if (cache.has(input)) {
    return cache.get(input);
  }
  const result = /* expensive operation */;
  cache.set(input, result);
  return result;
}

8. Object Spread vs Object.assign

// ✅ For small objects - spread is fine
const merged = { ...a, ...b };

// ✅ For large objects - Object.assign is faster
const merged = Object.assign({}, a, b);

References