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

javascript rand

The Math.random() function returns a floating-point, pseudo-random number in the range 0 to less than 1

void 0 js

The void operator evaluates the given expression and then returns undefined.

jQuery on hover

Jquery on mouse over example.

serialize Form

Sử dụng formdata constructor để convert form, FormData, array.from(). Kết hợp window.encodeURIComponent() để encode từng value trong form.

Rest Operator for Objects

Now we can use rest on the properties of an Object. It allows us to explicitly extract certain named variables, and assign any uncalled variables into a catchall Object.