Configs2026年4月11日·1 分钟阅读

Lodash — Modular Utility Library for JavaScript

Lodash is the most-downloaded utility library on npm. Modular helpers for arrays, objects, strings, collections, and functions — clone, debounce, throttle, merge, groupBy, and hundreds more battle-tested functions.

AI
AI Open Source · Community
快速使用

先拿来用,再决定要不要深挖

这里应该同时让用户和 Agent 知道第一步该复制什么、安装什么、落到哪里。

npm i lodash
# Or modular
npm i lodash.debounce lodash.throttle lodash.clonedeep
import _ from "lodash";

// Debounce expensive function
const search = _.debounce((q: string) => {
  fetch(`/api/search?q=${q}`);
}, 300);

// Deep clone
const copy = _.cloneDeep(original);

// Group and aggregate
const users = [{ age: 25, role: "dev" }, { age: 30, role: "dev" }, { age: 28, role: "pm" }];
const byRole = _.groupBy(users, "role");
const avgAge = _.meanBy(users, "age"); // 27.67

// Functional pipeline
const result = _.chain(users)
  .filter({ role: "dev" })
  .sortBy("age")
  .map("age")
  .value(); // [25, 30]
介绍

Lodash is a modern JavaScript utility library delivering modularity, performance, and extras. Created by John-David Dalton as a fork of Underscore.js focused on performance. 200+ utility functions covering arrays, objects, strings, collections, numbers, dates, functions, and more.

What Lodash Does

  • Array — chunk, difference, flatten, uniq, zip
  • Collection — each, filter, groupBy, map, reduce, sortBy
  • Function — debounce, throttle, memoize, once, partial
  • Lang — clone, cloneDeep, isEqual, isEmpty, merge
  • Object — get, set, pick, omit, invert, mapKeys
  • String — camelCase, kebabCase, startCase, template, truncate
  • Chain — fluent API for composing operations
  • FPlodash/fp provides auto-curried, immutable versions

Architecture

Highly optimized pure-JS functions, many with custom fast paths for common cases. Each function is publishable as its own npm package (lodash.debounce) for minimal bundles. The main lodash package is ~70KB gzipped but heavily tree-shakeable.

Self-Hosting

Client library — ships in bundle.

Key Features

  • 200+ utility functions
  • Tree-shakeable when imported by name
  • Separate tiny packages per function
  • Functional programming variant (lodash/fp)
  • Chaining API
  • TypeScript types (@types/lodash)
  • Battle-tested (30M+ projects use it)
  • Zero runtime dependencies

Comparison

Library Size Features Immutable TS
Lodash ~70KB 200+ fp variant via @types
Ramda ~50KB 200+ (FP only) Yes Yes
Radash ~10KB 100+ Yes Native
es-toolkit ~8KB 100+ Yes Native
Underscore ~15KB 100+ No via @types

常见问题 FAQ

Q: ES2023 了还需要 Lodash? A: 大部分简单操作可以用原生(Array.flat, Object.fromEntries),但 cloneDeep, debounce, isEqual, groupBy 原生还不够好或不存在。

Q: Bundle 太大? A: 按函数导入 import debounce from "lodash/debounce",或用 lodash-es + tree-shake。

Q: 有更现代的替代吗? A: 有。es-toolkit(TypeScript 原生、零依赖、更小)和 Radash 是新势力。

来源与致谢 Sources

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产