Skip to content

guoyunhe/fuck-lodash

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

33 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Fuck Lodash

You Know What I Mean.

This is a more complete version than You Don't Need Lodash/Underscore or You Might Not Need Lodash, with additional features:

A good lodash function alternative should be both smaller and faster. In some cases, here is no drop-in replacement, you have to modify your code a little bit.

Array

chunk

// πŸ“¦ 1.87 kB (gzip)
// πŸš€ 5.24 mHz
import chunk from 'lodash/chunk';

chunk(['a', 'b', 'c', 'd'], 2);
// πŸ“¦ 126 B (gzip), 93% smaller πŸ‘
// πŸš€ 7.41 mHz, 20% slower πŸ‘
const chunk = (arr, chunkSize = 1, cache = []) => {
  const tmp = [...arr];
  if (chunkSize <= 0) return cache;
  while (tmp.length) cache.push(tmp.splice(0, chunkSize));
  return cache;
};

chunk(['a', 'b', 'c', 'd'], 2);

compact

// πŸ“¦ 426 B (gzip)
// πŸš€ 5.52 mHz
import compact from 'lodash/compact';

compact([0, 1, false, 2, '', 3]);
// πŸ“¦ 53 B (gzip), 87% smaller πŸ‘
// πŸš€ 14.5 mHz, 163% faster πŸ‘
[0, 1, false, 2, '', 3].filter(Boolean);

concat

// πŸ“¦ 1.27 kB (gzip)
// πŸš€ 1.96 mHz
import concat from 'lodash/concat';

concat([1], 2, [3], [[4]]);
// πŸ“¦ 45 B (gzip), 96% smaller πŸ‘
// πŸš€ 3.63 mHz, 85% faster πŸ‘
[1].concat(2, [3], [[4]]);

difference

// πŸ“¦ 4.29 kB (gzip)
// πŸš€ 3.33 mHz
import difference from 'lodash/difference';

difference([2, 1], [3, 2]);
// πŸ“¦ 77 B (gzip), 98% smaller πŸ‘
// πŸš€ 12.1 mHz, 264% faster πŸ‘
const difference = (arr1, arr2) => arr1.filter((x) => !arr2.includes(x));

difference([2, 1], [3, 2]);

differenceBy

// πŸ“¦ 9.46 kB (gzip)
// πŸš€ 1.05 mHz
import differenceBy from 'lodash/differenceBy';

differenceBy([2.1, 1.2], [2.3, 3.4], Math.floor);
differenceBy([{ x: 2 }, { x: 1 }], [{ x: 1 }], 'x');
// πŸ“¦ 162 B (gzip), 98% smaller πŸ‘
// πŸš€ 2.58 mHz, 146% faster πŸ‘
const differenceBy = (arr1, arr2, iteratee) => {
  if (typeof iteratee === 'string') {
    const prop = iteratee;
    iteratee = (item) => item[prop];
  }
  return arr1.filter((c) => !arr2.map(iteratee).includes(iteratee(c)));
};

differenceBy([2.1, 1.2], [2.3, 3.4], Math.floor);
differenceBy([{ x: 2 }, { x: 1 }], [{ x: 1 }], 'x');

differenceWith

TODO

drop

// πŸ“¦ 1.42 kB (gzip)
// πŸš€ 4.67 mHz
import drop from 'lodash/drop';

drop([1, 2, 3], 2);
// πŸ“¦ 60 B (gzip), 96% smaller πŸ‘
// πŸš€ 9.67 mHz, 107% faster πŸ‘
const drop = (arr, n = 1) => arr.slice(n);

drop([1, 2, 3], 2);

Collection

Date

now

// πŸ“¦ 483 B (gzip)
// πŸš€ 4.95 mHz
import now from 'lodash/now';

now();
// πŸ“¦ 32 B (gzip), 93% smaller πŸ‘
// πŸš€ 11.4 mHz, 131% faster πŸ‘
Date.now();

Function

Lang

cloneDeep

// πŸ“¦ 7.38 kB (gzip)
// πŸš€ 0.39 mHz
import cloneDeep from 'lodash/cloneDeep';

const obj = {
  string: 'abcdef',
  number: 123.456,
  array: [
    {
      name: 'WebInspector',
      version: '537.36',
    },
    {
      name: 'WebInspector',
      version: '537.36',
    },
  ],
};

cloneDeep(obj);

rfdc is a fully featured alternative of lodash/deepClone. But it is bigger in size compared to other alternative.

// πŸ“¦ 1.15 kB (gzip), 84.4% smaller πŸ‘
// πŸš€ 1.06 mHz, 172% faster πŸ‘
import rfdc from 'rfdc';

const cloneDeep = rfdc();

const obj = {
  string: 'abcdef',
  number: 123.456,
  array: [
    {
      name: 'WebInspector',
      version: '537.36',
    },
    {
      name: 'WebInspector',
      version: '537.36',
    },
  ],
};

cloneDeep(obj);

If you only need to clone simple JSON data (no symbols, prototypes, etc.), klona/json is a lighter choice.

// πŸ“¦ 0.31 kB (gzip), 95.8% smaller πŸ‘
// πŸš€ 2.19 mHz, 456% faster πŸ‘
import { klona as cloneDeep } from 'klona/json';

const obj = {
  string: 'abcdef',
  number: 123.456,
  array: [
    {
      name: 'WebInspector',
      version: '537.36',
    },
    {
      name: 'WebInspector',
      version: '537.36',
    },
  ],
};

cloneDeep(obj);

isEqual

// πŸ“¦ 6.4 kB (gzip)
// πŸš€ 1.61 mHz
import isEqual from 'lodash/isEqual';

isEqual({ a: 1, b: 2 }, { b: 2, a: 1 });
// πŸ“¦ 609 B (gzip), 91% smaller πŸ‘
// πŸš€ 4.16 mHz, 158% faster πŸ‘
import isEqual from 'react-fast-compare';

isEqual({ a: 1, b: 2 }, { b: 2, a: 1 });

Math

Number

Object

Seq

String

Util

Properties

Methods