We use a javascript Map object to store computational expensive style functions which are called in order to style features in an Openlayers map render.
The style function requires a nested JSON object which is retrieved from each feature to be styled.
Map.has() will not recognise the JSON object due to the proto parameter. We can use stringify to create a 'Hashmap' key which can be evaluated in subsequent calls of the style function.
Our understanding is that JSON stringify is computational expensive and we would like to ask whether there are known alternatives to achieve the same as shown in this code but without the use of JSON stringify.
let memoizedStyles = new Map()
new ol.layer.VectorTile({
source: new ol.source.VectorTile(source),
style: feature => {
const style = feature.get('style')
const styleStr = JSON.stringify(style)
if (memoizedStyles.has(styleStr)) return memoizedStyles.get(styleStr)
const olStyle = new ol.style.Style({
stroke: style.strokeColor && new ol.style.Stroke({
color: style.strokeColor,
width: parseFloat(style.strokeWidth) || 1
}),
fill: style.fillColor && new ol.style.Fill({
color: style.fillColor
})
})
memoizedStyles.set(styleStr, olStyle)
return olStyle
}
})