grafana/k6 · error

empty name provided to SharedArray's constructor

Error message

empty name provided to SharedArray's constructor

What it means

The first argument to new SharedArray(name, fn) is the key used to register and share the array across VUs, so k6 requires a non-empty string. call.Argument(0).String() returns '' for an empty string (or a non-string value like a number/object coerced to nothing sensible), and the constructor throws immediately.

Solutions

  1. Pass a descriptive non-empty string as the first argument.
  2. Keep names unique per SharedArray in the script - two SharedArrays with the same name share one registry entry, and an empty name is simply rejected up front.
  3. If generating names dynamically, assert the computed name is non-empty before constructing.

Example fix

// before
const arr = new SharedArray('', () => [1, 2, 3]); // throws

// after
const arr = new SharedArray('user-ids', () => [1, 2, 3]);
Defensive patterns

Strategy: type-guard

Validate before calling

function assertSharedArrayName(name) {
  if (typeof name !== 'string' || name.trim() === '') {
    throw new Error(`SharedArray name must be a non-empty string, got: ${JSON.stringify(name)}`);
  }
  return name;
}

Type guard

const isValidSharedArrayName = (n) => typeof n === 'string' && n.length > 0;

Prevention

When it happens

Trigger: 'new SharedArray("", fn)', 'new SharedArray(null, fn)', or passing a non-string first argument whose String() conversion yields an empty string.

Common situations: Typos and copy-paste where the name placeholder was never filled in; programmatically generating SharedArrays in a loop and passing an empty variable; assuming the name is optional like in plain JS APIs.

Related errors


AI-assisted analysis of grafana/k6@01ffac6f24 (2026-08-18). Data as JSON: /api/errors/3306f68f1196756b. Report an issue: GitHub.

Appendix: source

Thrown at internal/js/modules/k6/data/data.go:83

			"SharedArray": d.sharedArray,
		},
	}
}

const asyncFunctionNotSupportedMsg = "SharedArray constructor does not support async functions as second argument"

// sharedArray is a constructor returning a shareable read-only array
// indentified by the name and having their contents be whatever the call returns
func (d *Data) sharedArray(call sobek.ConstructorCall) *sobek.Object {
	rt := d.vu.Runtime()

	if d.vu.State() != nil {
		common.Throw(rt, errors.New("new SharedArray must be called in the init context"))
	}

	name := call.Argument(0).String()
	if name == "" {
		common.Throw(rt, errors.New("empty name provided to SharedArray's constructor"))
	}
	val := call.Argument(1)

	if common.IsAsyncFunction(rt, val) {
		common.Throw(rt, errors.New(asyncFunctionNotSupportedMsg))
	}

	fn, ok := sobek.AssertFunction(val)
	if !ok {
		common.Throw(rt, errors.New("a function is expected as the second argument of SharedArray's constructor"))
	}

	builder := func() (sharedArray, error) { return getSharedArrayFromCall(rt, fn) }
	array, err := d.shared.loadOrStore(name, builder)
	if err != nil {
		common.Throw(rt, err)
	}

View on GitHub (pinned to 01ffac6f24)