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.1.0
A drop-in replacement for Underscore.js that delivers up to 8x performance improvements, bug fixes, and additional features.
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.
Documentation
The documentation for Lo-Dash can be viewed here: /doc/README.md
Underscore's documentation may also be used.
For a list of upcoming features, check out our roadmap.
So What's The Secret?
Lo-Dash's performance is gained by avoiding 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.
What else?
Lo-Dash comes with AMD loader support baked in, chainable _.each
, and will soon address cross-browser object iteration issues.
Installation and usage
In a browser:
<script src="lodash.js"></script>
Via npm:
npm install lodash
In Node.js and RingoJS v0.8.0+:
var _ = require('lodash');
In Narwhal and RingoJS v0.7.0-:
var _ = require('lodash')._;
In Rhino:
load('lodash.js');
In an AMD loader like RequireJS:
define.amd.lodash = true;
require({
'paths': {
'lodash': 'path/to/lodash'
}
},
['lodash'], function(_) {
console.log(_.VERSION);
});
Cloning this repo
To clone this repository including all submodules, using Git 1.6.5 or later:
git clone --recursive https://github.com/bestiejs/lodash.git
cd lodash.js
For older Git versions, just use:
git clone https://github.com/bestiejs/lodash.git
cd lodash
git submodule update --init
Feel free to fork and send pull requests if you see improvements!
Author
Contributors