according to https://developers.arcgis.com/javascript/latest/api-reference/esri-layers-Layer.html#id, the layer.id can be set so that it remains unique and, presumably static. This is not the numeric layerID value, but layer.id of the form 18d65c0f0b2-layer-29. Where is this changed?
You would include the id in the parameters passed to the layer's constructor, for example:
var layer = new FeatureLayer({
id: "myLayerID",
url: "https://myServer/arcgis/rest/services/MyService/MapServer/0"
});