Cache in map when possible

- To avoid possible xdmp:overflow error
- Update unit tests accordingly