March 2016
Core build ~4kB
Full build ~21kB
var names = [];
users.forEach(function (user) {
names.push(user.name);
});
// names = ['barney', 'fred']
var names = [];
_.forEach(users, function (user) {
names.push(user.name);
});
// names = ['barney', 'fred']
var users = [
{name: 'barney', age: 36},
{name: 'fred', age: 40}
];
// We want names = ['barney', 'fred']
var array = _.shuffle(_.range(1000));
Lodash 88% faster on my box
(MBP, Chrome/V8)
Native forEach handles more special cases (sparse arrays) and has more function lookups
array.forEach(function (val) {
return val;
});
_.forEach(array, function (val) {
return val;
});
https://jsperf.com/array-prototype-foreach-vs-each
https://jsperf.com/array-prototype-map-vs-map
https://jsperf.com/array-prototype-filter-vs-filter
https://jsperf.com/array-prototype-reduce-vs-reduce
Lodash > 80% faster than native Array.prototype methods: forEach(), map(), filter(), and reduce()
var names = [];
_.forEach(users, function (user) {
names.push(user.name);
});
// names = ['barney', 'fred']
var users = [
{name: 'barney', age: 36},
{name: 'fred', age: 40}
];
// We want names = ['barney', 'fred']
var names = _.map(users, function (user) {
return user.name;
});
var names = _.map(users, function (user) {
return user.name;
});
var names = _.map(users, 'name');
No more _.pluck() in Lodash V4
var data = {
response: {
code: '500',
method: 'GET',
url: {
href: 'http://...'
},
appl: {
code: '0',
msg: 'Invalid type'
}
}
};
if (data.response &&
data.response.appl &&
data.response.appl.msg) {
message = data.response.appl.msg;
}
if (_.has(data, 'response.appl.msg')) {
message = data.response.appl.msg;
}
message = _.get(data, 'response.appl.msg');
message = _.get(data, 'response.appl.msg', '');
var data = {
response: {
code: '500',
method: 'GET',
url: {
href: 'http://...'
},
appl: {
code: '0',
msg: 'Invalid type'
}
}
};
_.set(data, 'parsed.appl.msg', message);
data.parsed = {};
data.parsed.appl = {};
data.parsed.appl.msg = message;
data.parsed = {
appl: {
msg: message
}
};
if (myArray.length > 0) {
doSomething();
}
if (myArray) {
doSomething();
}
What I really want to do...
if (myArray.length > 0) {
doSomething();
}
if (!_.isEmpty(myArray)) {
doSomething();
}
if (myArray) {
doSomething();
}
What I really want to do...
Next best thing...
if (myArray.indexOf('foo') !== -1) {
doSomething();
}
if (_.includes(myArray, 'foo')) {
doSomething();
}
if ('foo' in myArray) {
doSomething();
}
What I really want to do...
Next best thing...
No more _.contains() in Lodash V4
*Testing for property (not value):
Testing for a value in an object is not very useful. Therefore, the utility of _.includes looks to be marginal once built-ins are fully implemented.
var firstUser = users[0];
var lastUser = users[users.length - 1];
var firstUser = _.first(users);
var lastUser = _.last(users);
var firstUser = users[0];
var lastUser = users[-1];
What I really want to do...
Next best thing...
var users = [
{name: 'barney', age: 36},
{name: 'fred', age: 40}
];
_.round(3.14159, 3); // 3.142
function round(num, precision) {
return Math.round(Number(num + 'e' + precision)) /
Number('1e' + precision);
}
round(3.14159, 3); // 3.142
_.max([12, 25, 3]); // 25
Math.max.apply(null, myArr); // 25
var myArr = [12, 25, 3];
myArr.reduce(function (prev, curr) {
return Math.max(prev, curr);
}); // 25
or
(large arrays can fail inconsistently)
var users = [
{name: 'barney', age: 36},
{name: 'fred', age: 40}
];
_.maxBy(users, 'age');
// {name: 'fred', age: 40}
Use _.maxBy() instead of _.max() in Lodash V4
var arr1 = ['a', 'a', 'b', 'c'];
var arr2 = ['b', 'c', 'd', 'e'];
_.uniq(arr1);
// ['a', 'b', 'c']
_.union(arr1, arr2);
// ['a', 'b', 'c', 'd', 'e']
_.intersection(arr1, arr2);
// ['b', 'c']
_.xor(arr1, arr2);
// ['a', 'd', 'e'];
_.difference(arr1, arr2);
// ['a', 'a'];
arr1
arr2
var animal = {
initAnimal: function () {},
eat: function () {},
poop: function () {}
};
var dog = Object.create(animal);
dog.initDog = function () {};
dog.bark = function () {};
dog.howl = function () {};
var animal = {
initAnimal: function () {},
eat: function () {},
poop: function () {}
};
var dog = _.create(animal, {
initDog: function () {},
bark: function () {},
howl: function () {}
});
var pig = function () {
return {
oink: function () {},
snort: function () {}
};
};
var dog = function () {
return {
bark: function () {},
howl: function () {}
};
};
var curly = Object.assign({}, pig(), dog());
Object.assign() is new with ES2015 (not fully supported)
var pig = function () {
return {
oink: function () {},
snort: function () {}
};
};
var dog = function () {
return {
bark: function () {},
howl: function () {}
};
};
var curly = _.assign({}, pig(), dog());
_.range(10);
// [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
for (var i = 0; i < 6; i++) {
$('#nextBtn').click();
}
_.times(6, function () {
$('#nextBtn').click();
});
_.countBy(users, 'category');
// {father: 2, mother: 2}
var users = [
{name: 'fred', category: 'father'},
{name: 'wilma', category: 'mother'},
{name: 'barney', category: 'father'},
{name: 'betty', category: 'mother'}
];
var users = [
{name: 'barney', age: 36},
{name: 'fred', age: 40},
{name: 'betty', age: 35},
{name: 'wilma', age: 39}
];
_.map(_.sortBy(users, 'age'), 'name');
// ['betty', 'barney', 'wilma', 'fred']
_.sortBy(users, 'age');
// [{name: 'betty', age: 35},
// {name: 'barney', age: 36},
// {name: 'wilma', age: 39},
// {name: 'fred', age: 40}]
var users = [
{name: 'fred', age: 40, gender: 'male'},
{name: 'wilma', age: 39, gender: 'female'},
{name: 'pebbles', age: 2, gender: 'female'},
{name: 'barney', age: 36, gender: 'male'},
{name: 'betty', age: 35, gender: 'female'},
{name: 'bamm bamm', age: 2, gender: 'male'}
];
// We want the youngest adult male
_.first(_.sortBy(_.filter(_.filter(
users,['gender', 'male']),
filterAdult), 'age')).name; // 'barney'
var result = _(users).filter(['gender', 'male'])
.filter(filterAdult)
.sortBy('age')
.first()
.name; // 'barney'
var users = [
{name: 'fred', age: 40, gender: 'male'},
{name: 'wilma', age: 39, gender: 'female'},
{name: 'pebbles', age: 2, gender: 'female'},
{name: 'barney', age: 36, gender: 'male'},
{name: 'betty', age: 35, gender: 'female'},
{name: 'bamm bamm', age: 2, gender: 'male'}
];
// We want the youngest adult male
angular.module('lodash', [])
.factory('_', function() {
return window._;
});
angular.module('lodash', [])
.factory('_', function() {
var lodash = window._;
// Don't delete if needed by 3rd party libs!!
delete window._;
return lodash;
});
angular.module('lodash', [])
.factory('_', function() {
var lodash = _.noConflict();
return lodash;
});