乐闻世界logo
搜索文章和话题

What is the difference between Lodash and native JavaScript? When should you use Lodash?

2月18日 21:40

In modern JavaScript development, both Lodash and native JavaScript provide many similar features. Here is a detailed answer comparing Lodash and native JavaScript:

Lodash vs Native JavaScript Comparison

1. Array Operations

map, filter, reduce

Native JavaScript:

javascript
const numbers = [1, 2, 3, 4, 5]; // map const doubled = numbers.map(n => n * 2); // => [2, 4, 6, 8, 10] // filter const evens = numbers.filter(n => n % 2 === 0); // => [2, 4] // reduce const sum = numbers.reduce((acc, n) => acc + n, 0); // => 15

Lodash:

javascript
import _ from 'lodash'; const numbers = [1, 2, 3, 4, 5]; // map const doubled = _.map(numbers, n => n * 2); // => [2, 4, 6, 8, 10] // filter const evens = _.filter(numbers, n => n % 2 === 0); // => [2, 4] // reduce const sum = _.reduce(numbers, (acc, n) => acc + n, 0); // => 15

Comparison: For these basic operations, native JavaScript and Lodash have similar functionality and performance.

Array Deduplication

Native JavaScript:

javascript
// ES6 Set const unique = [...new Set([1, 2, 2, 3, 4, 4, 5])]; // => [1, 2, 3, 4, 5] // filter + indexOf const unique = [1, 2, 2, 3, 4, 4, 5].filter((item, index, arr) => arr.indexOf(item) === index );

Lodash:

javascript
const unique = _.uniq([1, 2, 2, 3, 4, 4, 5]); // => [1, 2, 3, 4, 5] // Deduplicate object arrays by property const users = [ { id: 1, name: 'John' }, { id: 2, name: 'Jane' }, { id: 1, name: 'John' } ]; const uniqueUsers = _.uniqBy(users, 'id');

Comparison: Lodash's _.uniqBy() is more convenient when handling object arrays.

Array Grouping

Native JavaScript:

javascript
const people = [ { name: 'Alice', age: 25 }, { name: 'Bob', age: 25 }, { name: 'Charlie', age: 30 } ]; const grouped = people.reduce((acc, person) => { const key = person.age; if (!acc[key]) acc[key] = []; acc[key].push(person); return acc; }, {});

Lodash:

javascript
const grouped = _.groupBy(people, 'age'); // => { '25': [{...}, {...}], '30': [{...}] }

Comparison: Lodash's _.groupBy() is more concise and readable.

2. Object Operations

Deep Clone

Native JavaScript:

javascript
// JSON method (has limitations) const copy = JSON.parse(JSON.stringify(original)); // Structured clone (modern browsers) const copy = structuredClone(original); // Manual implementation function deepClone(obj) { if (obj === null || typeof obj !== 'object') return obj; if (obj instanceof Date) return new Date(obj); if (obj instanceof Array) return obj.map(item => deepClone(item)); const clonedObj = {}; for (let key in obj) { if (obj.hasOwnProperty(key)) { clonedObj[key] = deepClone(obj[key]); } } return clonedObj; }

Lodash:

javascript
const copy = _.cloneDeep(original);

Comparison: Lodash's _.cloneDeep() is simpler, more reliable, and supports more data types.

Safe Access to Nested Properties

Native JavaScript:

javascript
const user = { profile: { name: 'John' } }; // Optional chaining operator (ES2020) const name = user?.profile?.name; const defaultName = user?.profile?.name ?? 'default'; // Traditional approach (error-prone) const name = user && user.profile && user.profile.name;

Lodash:

javascript
const name = _.get(user, 'profile.name'); const defaultName = _.get(user, 'profile.name', 'default');

Comparison: Lodash's _.get() is more compatible with older browsers and provides more elegant default value support.

Object Merge

Native JavaScript:

javascript
// Shallow copy merge const merged = { ...obj1, ...obj2 }; // Object.assign const merged = Object.assign({}, obj1, obj2); // Deep merge (needs manual implementation) function deepMerge(target, source) { const output = { ...target }; if (isObject(target) && isObject(source)) { Object.keys(source).forEach(key => { if (isObject(source[key])) { if (!(key in target)) { Object.assign(output, { [key]: source[key] }); } else { output[key] = deepMerge(target[key], source[key]); } } else { Object.assign(output, { [key]: source[key] }); } }); } return output; }

Lodash:

javascript
const merged = _.merge({}, obj1, obj2);

Comparison: Lodash's _.merge() provides deep merging, simpler and more reliable.

3. Performance Comparison

Test Scenario: Array Operations

javascript
// Test data const largeArray = Array.from({ length: 100000 }, (_, i) => i); // map performance test console.time('native map'); largeArray.map(n => n * 2); console.timeEnd('native map'); console.time('lodash map'); _.map(largeArray, n => n * 2); console.timeEnd('lodash map');

Result: In most cases, native JavaScript performs slightly better than Lodash, but the difference is not significant.

Test Scenario: Deep Clone

javascript
const largeObject = { a: 1, b: { c: 2, d: { e: 3 } } }; console.time('native JSON'); JSON.parse(JSON.stringify(largeObject)); console.timeEnd('native JSON'); console.time('lodash cloneDeep'); _.cloneDeep(largeObject); console.timeEnd('lodash cloneDeep');

Result: Lodash's _.cloneDeep() is usually faster than JSON.parse(JSON.stringify()) and supports more data types.

4. Browser Compatibility

FeatureNative JavaScriptLodash
Array.prototype.mapES5 (IE9+)All browsers
Array.prototype.filterES5 (IE9+)All browsers
Array.prototype.reduceES5 (IE9+)All browsers
Optional chainingES2020 (no IE support)All browsers
Nullish coalescingES2020 (no IE support)All browsers
Structured cloneModern browsersAll browsers

5. Code Readability

Native JavaScript:

javascript
const result = users .filter(user => user.age > 18) .map(user => ({ id: user.id, name: user.name.toUpperCase() })) .sort((a, b) => a.name.localeCompare(b.name));

Lodash:

javascript
const result = _.chain(users) .filter(user => user.age > 18) .map(user => ({ id: user.id, name: _.toUpper(user.name) })) .orderBy('name') .value();

Comparison: Lodash's method chaining is more readable in some scenarios, but native JavaScript's method chaining is also clear.

6. Bundle Size

Native JavaScript: No additional size

Lodash:

  • Full import: ~70KB (gzipped)
  • On-demand import: ~1-2KB per method
  • Using lodash-es: Supports Tree-shaking
javascript
// Full import (not recommended) import _ from 'lodash'; // On-demand import (recommended) import cloneDeep from 'lodash/cloneDeep'; import debounce from 'lodash/debounce';

7. When to Use Lodash?

Recommended scenarios for Lodash:

  1. Need to support older browsers
  2. Need complex operations like deep clone, deep merge
  3. Need method chaining for complex data transformations
  4. Team is already familiar with Lodash API
  5. Need utility functions like debounce, throttle

Recommended scenarios for native JavaScript:

  1. Only need basic array, object operations
  2. Pursue minimal bundle size
  3. Target browsers support modern JavaScript features
  4. Project has high performance requirements

8. Best Practices

javascript
// Mixed usage example import { debounce, throttle } from 'lodash'; class SearchComponent { constructor() { this.debouncedSearch = debounce(this.performSearch.bind(this), 300); } handleInput(event) { const value = event.target.value; // Use native methods for simple operations const trimmed = value.trim(); const normalized = trimmed.toLowerCase(); // Use Lodash for complex operations if (normalized) { this.debouncedSearch(normalized); } } performSearch(keyword) { // Use native methods const results = this.data.filter(item => item.name.toLowerCase().includes(keyword) ); // Use Lodash methods const sortedResults = _.orderBy(results, ['score'], ['desc']); this.displayResults(sortedResults); } }

Summary

Lodash and native JavaScript each have their advantages:

  • Native JavaScript: Better performance, smaller size, more modern API
  • Lodash: Better compatibility, richer API, some operations are more concise

In actual projects, it's recommended to:

  1. Prioritize native JavaScript ES6+ features
  2. Use Lodash when needing complex operations or compatibility with older browsers
  3. Import Lodash methods on demand to avoid importing the entire library
  4. Make choices based on team habits and project requirements
标签:Lodash