Skip to content

Instantly share code, notes, and snippets.

@adoyle-h adoyle-h/test.js
Created Dec 7, 2017

Embed
What would you like to do?
bad lodash iterations
const _ = require('lodash');
const hasLength = {
length: 4,
a: 1,
b: 2,
};
const normal = {
a: 1,
b: 2,
};
function print(tag) {
return (v, k) => console.log(`[${tag}] val=${v}, key=${k}`);
}
['each', 'map', 'every', 'filter', 'values'].forEach((method) => {
_[method](hasLength, print(`${method} - hasLength`));
_[method](normal, print(`${method} - normal`));
});
// It will print:
// [each - hasLength] val=undefined, key=0
// [each - hasLength] val=undefined, key=1
// [each - hasLength] val=undefined, key=2
// [each - hasLength] val=undefined, key=3
// [each - normal] val=1, key=a
// [each - normal] val=2, key=b
// [map - hasLength] val=undefined, key=0
// [map - hasLength] val=undefined, key=1
// [map - hasLength] val=undefined, key=2
// [map - hasLength] val=undefined, key=3
// [map - normal] val=1, key=a
// [map - normal] val=2, key=b
// [every - hasLength] val=undefined, key=0
// [every - normal] val=1, key=a
// [filter - hasLength] val=undefined, key=0
// [filter - hasLength] val=undefined, key=1
// [filter - hasLength] val=undefined, key=2
// [filter - hasLength] val=undefined, key=3
// [filter - normal] val=1, key=a
// [filter - normal] val=2, key=b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.