The Lodash documentation says that it supports lazy evaluation. From my testing, the below chain is being evaluated 100 times rather than 10. I'm using version 3.10.1.
_(_.range(100))
.map(function(x) {console.log(1); return x; })
.take(10)
.value()
You can see that we print to the console 100 times inside map
, rather than the 10 times I would have expected. Check out the problem here: /
What am I doing wrong? How can I make this evaluate lazily?
Update: This appears to be a regression in Lodash. I tested how this works across versions and came across the following results:
Version 2.4.2: 100 times /
Version 3.0.0: 10 times /
Version 3.9.0 10 times /
Version 3.10.0: 100 times /
The Lodash documentation says that it supports lazy evaluation. From my testing, the below chain is being evaluated 100 times rather than 10. I'm using version 3.10.1.
_(_.range(100))
.map(function(x) {console.log(1); return x; })
.take(10)
.value()
You can see that we print to the console 100 times inside map
, rather than the 10 times I would have expected. Check out the problem here: https://jsfiddle/07utwk6y/
What am I doing wrong? How can I make this evaluate lazily?
Update: This appears to be a regression in Lodash. I tested how this works across versions and came across the following results:
Version 2.4.2: 100 times https://jsfiddle/4Lq7z5xL/
Version 3.0.0: 10 times https://jsfiddle/fd6g6un5/
Version 3.9.0 10 times https://jsfiddle/ju8rppee/
Version 3.10.0: 100 times https://jsfiddle/x1g13oo8/
Share Improve this question edited Aug 12, 2015 at 22:49 inperspective asked Aug 8, 2015 at 5:27 inperspectiveinperspective 1,9041 gold badge20 silver badges22 bronze badges1 Answer
Reset to default 11This is expected behavior. Lodash will only perform this optimization on arrays with 200 or more items. Increase the range to 200 in the first line to see this working.