Lodash sort - Test performance with native js

javascript,benchmark,performance,es6,nativejs

Lodash sort - benchmark

Script Preparation code:

var fruits = [
  {name:"banana", amount: 2},
  {name:"apple", amount: 4},
  {name:"pineapple", amount: 2},
  {name:"mango", amount: 1}
];

Use lodash sort:

_.orderBy(fruits, ['name'],['asc']);

Use native sort:

sortBy = (key) => {
  return (a, b) => (a[key] > b[key]) ? 1 : ((b[key] > a[key]) ? -1 : 0);
};

fruits.concat().sort(sortBy("name"));

Test case nameResult

lodash orderby    lodash orderby x 858,076 ops/sec ±1.86% (62 runs sampled)

native sort    native sort x 1,483,421 ops/sec ±0.48% (65 runs sampled)

Fastest: native sort

Slowest: lodash orderby

Ref: Trường hợp nào nên sử dụng LODASH

Có thể bạn đã miss một số snippets code

Filter Unique Values

Kiểu đối tượng Set đã được giới thiệu trong ES6 và cùng với ..., ‘spread’ operator, chúng ta có thể sử dụng nó để tạo một mảng mới chỉ với các giá trị duy nhất.

Spread Operator for Objects

Using the spread operator during an Object declaration will assign the properties of the referenced Object to the new Object.

group by property javascript use reduce

group by property javascript use reduce

using async/await to catch errors

Using promises to catch errors