What is lodash?
Lodash is a JavaScript library that provides utility functions for common programming tasks using a functional programming paradigm. It includes functions for manipulating and traversing arrays, objects, and strings, as well as utilities for functions, language, math, number, object, sequence, and utility methods.
What are lodash's main functionalities?
Array Manipulation
Lodash provides a rich set of array manipulation functions such as map, filter, find, and sort. The code sample demonstrates sorting an array in ascending order using a custom comparator.
[3, 2, 1].sort(_.compareWith(function(a, b) { return a - b; }))
Object Manipulation
Lodash allows for easy manipulation and traversal of objects. The code sample shows how to assign properties from source objects to a destination object.
_.assign({ 'a': 1 }, { 'b': 2 }, { 'c': 3 })
String Manipulation
Lodash includes functions to manipulate strings, such as converting to different cases, trimming, padding, etc. The code sample demonstrates converting a string to kebab-case.
_.kebabCase('Foo Bar')
Function Utilities
Lodash provides function utilities like debounce and throttle to control function invocation. The code sample shows a debounced function that will only be invoked after 250 milliseconds have passed without it being called again.
_.debounce(function() { console.log('Debounced'); }, 250)
Language Utilities
Lodash includes utilities for deep cloning, merging, and comparing objects. The code sample demonstrates deep cloning an object to ensure nested objects are cloned as well.
_.cloneDeep({ 'a': 1, 'b': { 'c': 2 } })
Other packages similar to lodash
underscore
Underscore is a utility library with similar functionality to Lodash, offering a range of functions for manipulating arrays, objects, and functions. It is generally considered to be the predecessor to Lodash, which provides a superset of Underscore's features with additional performance optimizations.
ramda
Ramda is a functional programming library that emphasizes a more functional and composable approach compared to Lodash. It provides similar utilities but focuses on immutability and side-effect free functions, which can lead to a different programming style.
immutable
Immutable.js offers a different take on data manipulation by providing persistent immutable data structures. Unlike Lodash, which works with standard JavaScript objects and arrays, Immutable.js uses its own data structures, which can lead to better performance and easier reasoning about state changes in certain applications.
Lo-Dash v0.5.0
A drop-in replacement* for Underscore.js, from the devs behind jsPerf.com, delivering performance, bug fixes, and additional features.
Lo-Dash’s performance is gained by avoiding slower native methods, instead opting for simplified non-ES5 compliant methods optimized for common usage, and by leveraging function compilation to reduce the number of overall function calls.
Download
Dive in
We’ve got API docs, benchmarks, and unit tests.
Create your own benchmarks at jsPerf, or search for existing ones.
For a list of upcoming features, check out our roadmap.
Screencasts
For more information check out these screencasts over Lo-Dash:
Features
Support
Lo-Dash has been tested in at least Chrome 5-21, Firefox 1.5-13, IE 6-9, Opera 9.25-12.01, Safari 3-6, Node.js 0.4.8-0.8.7, Narwhal 0.3.2, RingoJS 0.8, and Rhino 1.7RC5.
Custom builds
Custom builds make it easy to create lightweight versions of Lo-Dash containing only the methods you need.
We handle all the method dependency and alias mapping for you.
- Backbone builds, containing all methods required by Backbone, may be created using the
backbone
modifier argument.
lodash backbone
- CSP builds, supporting default Content Security Policy restrictions, may be created using the
csp
modifier argument.
lodash csp
- Legacy builds, tailored for older browsers without ES5 support, may be created using the
legacy
modifier argument.
lodash legacy
- Mobile builds, with IE < 9 bug fixes and method compilation removed, may be created using the
mobile
modifier argument.
lodash mobile
- Strict builds, with
_.bindAll
, _.defaults
, and _.extend
in strict mode, may be created using the strict
modifier argument.
lodash strict
- Underscore builds, containing only methods included in Underscore, may be created using the
underscore
modifier argument.
lodash underscore
Custom builds may be created in three ways:
- Use the
category
argument to pass the categories of methods to include in the build.
Valid categories are "arrays", "chaining", "collections", "functions", "objects", and "utilities".
lodash category=collections,functions
lodash category="collections, functions"
- Use the
exclude
argument to pass the names of methods to exclude from the build.
lodash exclude=union,uniq,zip
lodash exclude="union, uniq, zip"
- Use the
include
argument to pass the names of methods to include in the build.
lodash include=each,filter,map
lodash include="each, filter, map"
All arguments, except backbone
with underscore
, exclude
with include
, and legacy
with csp
/mobile
, may be combined.
lodash backbone legacy category=utilities exclude=first,last
lodash underscore mobile strict category=functions include=pick,uniq
The lodash
command-line utility is available when Lo-Dash is installed as a global package (i.e. npm install -g lodash
).
Custom builds are saved to lodash.custom.js
and lodash.custom.min.js
.
Installation and usage
In browsers:
<script src="lodash.js"></script>
Using npm:
npm install lodash
npm install -g lodash
In Node.js and RingoJS v0.8.0+:
var _ = require('lodash');
In RingoJS v0.7.0-:
var _ = require('lodash')._;
In Rhino:
load('lodash.js');
In an AMD loader like RequireJS:
require({
'paths': {
'underscore': 'path/to/lodash'
}
},
['underscore'], function(_) {
console.log(_.VERSION);
});
Resolved Underscore.js issues (20+)
- Allow iteration of objects with a
length
property [#148, #154, #252, #448, #659, test] - Ensure array-like objects with invalid
length
properties are treated like regular objects [test] - Ensure "Arrays" methods allow falsey
array
arguments [test] - Ensure "Collections" methods allow string
collection
arguments [#247, #276, #561, test] - Ensure templates compiled with errors are inspectable [#666, test]
- Fix cross-browser object iteration bugs [#60, #376, test]
- Handle arrays with
undefined
values correctly in IE < 9 [#601] - Methods should work on pages with incorrectly shimmed native methods [#7, test]
- Register as an AMD module, but still export to global [#431, test]
_(…)
should return passed wrapper instances [test]_.clone
should allow deep
cloning [#595, test]_.contains
should work with strings [#667, test]_.escape
should return an empty string when passed null
or undefined
[#427, test]_.extend
should recursively extend objects [#379, test]_.forEach
should be chainable [#142, test]_.forEach
should allow exiting iteration early [#211, test]_.groupBy
should add values to own, not inherited, properties [test]_.isEmpty
and _.size
should support jQuery/MooTools DOM query collections [#690, test]_.isEqual
should return true
for like-objects from different documents [test]_.isObject
should avoid V8 bug #2291 [#605, test]_.isNaN(new Number(NaN))
should return true
[test]_.keys
and _.size
should work with arguments
objects cross-browser [#396, #653, test]_.once
should free the given function for garbage collection [#693]_.range
should coerce arguments to numbers [#634, #683, test]_.reduceRight
should pass correct callback arguments when iterating objects [test]_.size
should return the length
of string values [test]_.size
shouldn’t error on falsey values [#650, test]_.sortedIndex
should support arrays with high length
values [test]_.template
should not augment the options
object [test]_.throttle
should work when called in a loop [#502, test]_.zipObject
should accept less than two arguments [test]
Optimized methods (50+)
_.bind
_.bindAll
_.clone
_.compact
_.contains
, _.include
_.defaults
_.defer
_.difference
_.each
_.escape
_.every
, _.all
_.extend
_.filter
, _.select
_.find
, _.detect
_.flatten
_.forEach
, _.each
_.functions
, _.methods
_.groupBy
_.indexOf
_.intersection
_.invoke
_.isArguments
_.isDate
_.isEmpty
_.isFinite
_.isFunction
_.isObject
_.isNumber
_.isRegExp
_.isString
_.keys
_.lastIndexOf
_.map
, _.collect
_.max
_.memoize
_.min
_.mixin
_.pick
_.pluck
_.reduce
, _.foldl
, _.inject
_.reject
_.result
_.shuffle
_.some
, _.any
_.sortBy
_.sortedIndex
_.template
_.throttle
_.times
_.toArray
_.union
_.uniq
, _.unique
_.values
_.without
_.wrap
_.zip
- plus all
_(…)
method wrappers
Release Notes
v0.5.0
- Added _.countBy, _.drop, _.merge, and _.where
- Added
csp
(Content Security Policy) and underscore
build options - Added
deep
cloning support to _.clone
- Added Jam package support
- Added support for exiting
_.forEach
, _.forIn
, and _.forOwn
early by returning false
in the callback
- Added support for jQuery/MooTools DOM query collections to
_.isEmpty
and _.size
- Ensured development build works with IE conditional compilation enabled
- Ensured
_.clone
doesn't clone functions, DOM nodes, arguments
objects, and objects created by constructors other than Object
- Ensured
_.filter
’s callback
can't modify result values - Ensured
_.isEmpty
, _.isEquals
, and _.size
support arguments
objects - Ensured
_.isEqual
doesn't inspect DOM nodes, works with objects from other documents, and calls custom isEqual
methods before checking strict equality - Ensured
_.once
frees the given function for garbage collection - Ensured
_.sortBy
performs a stable sort - Ensured
reEvaluateDelimiter
is assigned when _.templateSettings.evaluate
is undefined - Made
_.range
coerce arguments to numbers - Optimized
_.isFunction
The full changelog is available here.
BestieJS
Lo-Dash is part of the BestieJS "Best in Class" module collection. This means we promote solid browser/environment support, ES5 precedents, unit testing, and plenty of documentation.
Author
Contributors