Skip to content

Instantly share code, notes, and snippets.

View bjruberg's full-sized avatar

Björn Ruberg bjruberg

View GitHub Profile
Lodash Function Bytes minified and gzipped
isNil.gz 496
isNull.gz 496
isObjectLike.gz 499
isUndefined.gz 499
defaultTo.gz 501
eq.gz 502
head.gz 505
isObject.gz 509
first.gz 512
// native
array.map(item => item.field)
array.filter(item => item.value === 1)
array.sort(item => item.value)
// lodash's shorter variants
_.map(array, "field")
_.filter(array, { value: 1 })
_.sort(array, "value")
// native variant needs to protect against null values
const data = null
const result = Array.isArray(data) ? data.map(item => parseInt(item) : []
// not so with lodash
const result = _.map(null, item => parseInt(item))
// no exception, result is []
// native variant
const objectValues = Object.values(dataObject)
const result = objectValues.map(item => parseInt(item))
// same as this lodash variant with implict conversion
const result = _.map(dataObject, item => parseInt(item))
import { chain, filter, map, sortBy, uniqBy, compact } from "lodash";
const messageList = [{ user: 1, messageId: 1, creationDate: new Date(), text: "Hello" }];
chain(messageList)
.filter({ user: 1 })
.sortBy("creationDate")
.uniqBy("messageId")
.map("text")
.compact()
import { uniqBy } from "lodash"
uniqBy(someCollection, "value")
const prepareArray = (arr, field) =>
if Array.isArray(arr) {
return arr.reduce((res, cur) => (res.length === 0 || cur[field] !== res[res.length - 1][field] ? [...res, cur] : res), [])
}
return []
}
prepareArray(someDataCollection, "value")