Skip to content

Instantly share code, notes, and snippets.

@adoyle-h
Created December 7, 2017 03:58
Show Gist options
  • Save adoyle-h/2e13e4668c7df54c04c1a551d14d98b9 to your computer and use it in GitHub Desktop.
Save adoyle-h/2e13e4668c7df54c04c1a551d14d98b9 to your computer and use it in GitHub Desktop.
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