Skip to content
Back to skills

Js Length Check First

ASecurity

Imported skill js_length_check_first from vercel

  • 3 stars
  • 0 votes
  • 0 copies
  • 3 views
  • Added September 11, 2026
developmentjavascripttypescriptjavareactperformance

Security analysis

A100/100

Scanned September 11, 2026

npx -y skills add bitwikiorg/skills.md --skill js_length_check_first --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Js Length Check First?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Js Length Check First
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/bitwikiorg-js-length-check-first/badge)](https://www.skillsdirectory.com/skills/bitwikiorg-js-length-check-first)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
description: Imported skill js_length_check_first from vercel
name: js_length_check_first
signature: 8b54a311c826b299272f29187fc58bc55772a04c4a72eeb29456ee96dc9fc624
source: /a0/tmp/skills_research/vercel/skills/react-best-practices/rules/js-length-check-first.md
---

---
title: Early Length Check for Array Comparisons
impact: MEDIUM-HIGH
impactDescription: avoids expensive operations when lengths differ
tags: javascript, arrays, performance, optimization, comparison
---

## Early Length Check for Array Comparisons

When comparing arrays with expensive operations (sorting, deep equality, serialization), check lengths first. If lengths differ, the arrays cannot be equal.

In real-world applications, this optimization is especially valuable when the comparison runs in hot paths (event handlers, render loops).

**Incorrect (always runs expensive comparison):**

```typescript
function hasChanges(current: string[], original: string[]) {
  // Always sorts and joins, even when lengths differ
  return current.sort().join() !== original.sort().join()
}
```

Two O(n log n) sorts run even when `current.length` is 5 and `original.length` is 100. There is also overhead of joining the arrays and comparing the strings.

**Correct (O(1) length check first):**

```typescript
function hasChanges(current: string[], original: string[]) {
  // Early return if lengths differ
  if (current.length !== original.length) {
    return true
  }
  // Only sort when lengths match
  const currentSorted = current.toSorted()
  const originalSorted = original.toSorted()
  for (let i = 0; i < currentSorted.length; i++) {
    if (currentSorted[i] !== originalSorted[i]) {
      return true
    }
  }
  return false
}
```

This new approach is more efficient because:
- It avoids the overhead of sorting and joining the arrays when lengths differ
- It avoids consuming memory for the joined strings (especially important for large arrays)
- It avoids mutating the original arrays
- It returns early when a difference is found

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.