In lodash.js, does it cache the result for the `.value ()` method?

For example, I have codes (coffeescript) as follows:

sortedLatLng = _(w) .sortBy (x) -> x.time .map (x) -> [x.longitude,x.latitude] .uniq((x)-> x[0].toFixed(3) + "," + x[1].toFixed(3)) # keep three decimal to merge nearby points console.log(sortedLatLng.value()) myFunction1(sortedLatLng.value()) myFunction2(sortedLatLng.value()) console.log(sortedLatLng.reverse().value()) 

This can be chained by another lodash method later. Meanwhile, its meaning may need to be extracted. I just wondered if it would cache the result. I did not find how this is implemented in his documentation ..

Will it be calculated once or twice for:

 myFunction1(sortedLatLng.value()) myFunction2(sortedLatLng.value()) 

Does anyone have any ideas about this?

+6
source share
1 answer

When you create the lodash shell, the wrapped value is stored in the shell. For instance:

 var wrapper = _([ 1, 2, 3 ]); 

Here [ 1, 2, 3 ] is stored in a wrapper , and any related operations added to the shell are passed by this value. Chain operations are stored, not executed. For instance:

 var wrapper = _([ 1, 2, 3 ]).map(function(item) { console.log('mapping'); return item; }); 

This code creates a wrapper with the map() operation, but does not execute it. Instead, it stores chain operations, so when value() is called, it can execute them:

 var wrapper = _([ 1, 2, 3 ]).map(function(item) { console.log('mapping'); return item; }); wrapper.value() // mapping // ... 

Calling value() again on this shell will simply repeat the same operations on the wrapped value - the results are not cached.

+1
source

Source: https://habr.com/ru/post/987710/


All Articles