Object Returned from SharedArray Frozen?

Out of curiosity, when I extract an object from SharedArray described in the example I am not able to modify the object itself. Is the object I assume readonly or frozen?

IE: if I do something like:
const data = loadData("/resources/api/foo/bar.json");

I cannot modify data[0].whatever (a property in the .json object). FWIW loadData is just a function that does:

export function loadData(dataFile) {
  const data = new SharedArray('dataFile', function () {
    const obj = JSON.parse(open(dataFile));
    return obj; 
  })
  return data
}

Am I correct in assuming the referenced object that sharedArray returns is NOT writable? I assume I can get around this via Object.assign to a new object. But I wanted to just get a verification.

Thanks!

Hi @mercfh

You are correct, a SharedArray is indeed read-only, as documented in https://k6.io/docs/javascript-api/k6-data/sharedarray/:smile:

Once constructed, a SharedArray is read-only, so you can’t use a SharedArray to communicate data between VUs.

Cheers!

Thanks. For others with the same issue I got around this by using Object.Assign to an empty object.

This is useful if i’m storing multiple “pieces” that are needed from a .json file and need to alter it.

1 Like