I have an Arcade calculation attribute rule that __sets polyline M-values__ to the __cumulative length of the line__.

The script works as expected. But I'm still a novice, so I thought I'd ask: __Can the script be improved?__

*Notes about the script: *

*- It works for both singlepart and multipart features.*

*- It densifies true curves (unavoidable, since Arcade pre-densifies true curves).*

- The code below is out-of-date. I've posted an updated version in a reply...further down in this post.

//var paths = [[[0, 5, null], [10, 10, null], [30, 0, null], [50, 10, null], [60, 10, null]]]; //JS
//Only do the calculation if the geometry was changed/edited.
if (!Equals(Geometry($feature), Geometry($originalfeature))) {
var geom = Dictionary(Text(Geometry($feature)));
var paths = geom['paths'];
//Alternatively, this could be done with the Arcade distance() function. I chose to do the math manually so that the script can be used elsewhere, such as VSCode (for debugging).
function pythagoras(x1, y1, x2, y2) {
return sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2));
}
for (var path_idx in paths) {
var oldX = paths[0][0][0], oldY = paths[0][0][1], length = 0;
for (var point_idx in paths[path_idx]) {
var newX = paths[path_idx][point_idx][0], newY = paths[path_idx][point_idx][1];
length += pythagoras(oldX, oldY, newX, newY);
paths[path_idx][point_idx][2] = length;
oldX = newX;
oldY = newY;
}
}
console(paths);
return { "result": { "geometry": Polyline(geom) } };
//Output: [[[0,5,0],[10,10,11.180339887498949],[30,0,33.54101966249685],[50,10,55.90169943749475],[60,10,65.90169943749476]]]
}

(The script was adapted from this GitHub sample: Set Ms to Index.)

Thanks!