最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Lazy Evaluation Not Working In Lodash - Stack Overflow

programmeradmin7浏览0评论

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 badges
Add a ment  | 

1 Answer 1

Reset to default 11

This 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.

发布评论

评论列表(0)

  1. 暂无评论