Back to all skills
Progress24 of 45
JavaScriptLow

Use Loop for Min/Max Instead of Sort

Finding the smallest or largest element only requires a single pass through the array. Sorting is wasteful and slower.

javascriptarraysperformancesortingalgorithms

Code Comparison

Incorrect (O(n log n) - sort to find latest):typescript
Incorrect (O(n log n) - sort for oldest and newest):typescript
Correct (O(n) - single loop):typescript
Alternative (Math.min/Math.max for small arrays):typescript

Why This Matters

Impact: O(n) instead of O(n log n). This optimization is classified as LOW priority for production applications.