Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> JavaScript

Khác biệt giữa Lodash và Underscore

Cả 2 đều **utility library** hỗ trợ array, object, string, function... giúp code JS ngắn gọn, functional.

Mối quan hệ

  • Lodash là **fork của Underscore**, API tương thích (drop-in replacement).
  • Nhưng Lodash **rewrite hoàn toàn** nội bộ, thêm nhiều feature, performance tốt hơn.

Lodash vượt trội hơn

  • Consistent cross-env iteration: Array, string, object, arguments object.
  • Extra features: _.cloneDeep, _.merge (deep), _.debounce, _.throttle, _.memoize...
  • Performance: Tối ưu large array/object iteration.
  • Custom builds: Tree-shaking, chỉ bundle method dùng到.
  • Template pre-compilation: _.template compile sẵn.
  • AMD/ESM support: Module system tốt hơn.

Khi nào dùng Underscore?

  • Project legacy đã dùng Underscore, không muốn migrate.
  • Bundle size cực nhỏ (Underscore ~6KB, Lodash full ~70KB — nhưng custom build Lodash nhỏ hơn).

Khuyến nghị: Dùng **Lodash** (hoặc lodash-es cho ESM tree-shaking).