DEV Community

Alex Spinov
Alex Spinov

Posted on

9 JavaScript One-Liners That Replace Entire Libraries

Stop installing packages for things JavaScript can do natively

Every npm package adds weight, dependencies, and potential vulnerabilities. Here are 9 things you might be importing a library for — that you can do in one line.


1. Deep Clone (replaces lodash.cloneDeep)

const clone = structuredClone(originalObject);
Enter fullscreen mode Exit fullscreen mode

Built into every modern browser and Node 17+. Handles nested objects, arrays, dates, maps, sets.


2. UUID Generation (replaces uuid)

const id = crypto.randomUUID();
// '3b241101-e2bb-4d7a-8613-e0c07a8a3c59'
Enter fullscreen mode Exit fullscreen mode

Built into browsers and Node 19+. No package needed.


3. Debounce (replaces lodash.debounce)

const debounce = (fn, ms) => { let t; return (...a) => { clearTimeout(t); t = setTimeout(() => fn(...a), ms); }; };
Enter fullscreen mode Exit fullscreen mode

4. Flatten Array (replaces lodash.flatten)

const flat = nestedArray.flat(Infinity);
// [1, [2, [3, [4]]]] → [1, 2, 3, 4]
Enter fullscreen mode Exit fullscreen mode

5. Unique Array (replaces lodash.uniq)

const unique = [...new Set(array)];
Enter fullscreen mode Exit fullscreen mode

6. Group By (replaces lodash.groupBy)

const grouped = Object.groupBy(users, user => user.role);
// { admin: [...], user: [...] }
Enter fullscreen mode Exit fullscreen mode

Available in Node 21+ and modern browsers.


7. Sleep / Delay (replaces various timer packages)

const sleep = ms => new Promise(r => setTimeout(r, ms));
await sleep(1000); // Wait 1 second
Enter fullscreen mode Exit fullscreen mode

8. Query String Parsing (replaces qs)

const params = Object.fromEntries(new URLSearchParams('name=Alex&role=dev'));
// { name: 'Alex', role: 'dev' }
Enter fullscreen mode Exit fullscreen mode

9. Date Formatting (replaces moment/dayjs for simple cases)

new Date().toLocaleDateString('en-US', { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' });
// 'Wednesday, March 26, 2026'
Enter fullscreen mode Exit fullscreen mode

For complex date math, you still need a library. But for formatting? Intl handles it.


When to still use the library

  • lodash: If you need 10+ utility functions, importing the whole thing is fine
  • date-fns/dayjs: If you're doing date math (add days, diff, timezones)
  • uuid: If you need v1/v5 UUIDs specifically (crypto.randomUUID is v4 only)

But for most projects? These one-liners are enough.


What npm package did you recently realize you didn't need?


I write about developer productivity and build automation tools. Follow for more.


More from me: 10 Dev Tools I Use Daily | 77 Scrapers on a Schedule | 150+ Free APIs

Top comments (0)