procedure TControl.Loaded;
var
  i          : Integer;
  NodeList   : TJSNodeList; // live collection of child nodes
  NodeLen    : Integer;     // cached length of the list
  CurIdx     : Integer;     // current index of the node we are moving
  TargetIdx  : Integer;     // requested ChildOrder, possibly adjusted
  RefNode    : TJSNode;     // node that will be the insertBefore anchor
  TargetNode : TJSNode;     // the child’s DOM container
  C          : TControl;    // local alias for Controls[i]
begin
  // skip when the control is not in loading state
  if not (csLoading in ComponentState) then
    Exit;

  inherited;

  // ---------- 1. reorder relative children if we have a container ----------
  if Assigned(ChildContainer) then
  begin
    NodeList := ChildContainer.childNodes;  // cache list reference
    NodeLen  := NodeList.Length;            // cache list length

    for i := 0 to ControlCount - 1 do
    begin
      C := Controls[i];

      // qualify for reordering: relative or ignore, not linked, ChildOrder >= 0
      if not ((C.ElementPosition in [epRelative, epIgnore]) and
              (not C.IsLinked) and
              (C.ChildOrder >= 0)) then
      begin
        C.Loaded;                           // still finalize the child
        Continue;
      end;

      TargetIdx  := C.ChildOrder;
      TargetNode := C.Container;

      // fast path: want a slot beyond current list -> simple append
      if TargetIdx >= NodeLen then
      begin
        ChildContainer.appendChild(TargetNode);
        C.Loaded;
        Continue;
      end;

      // slow path: node may need compensation if moving forward
      asm
        // Find current index of TargetNode inside NodeList
        CurIdx = Array.prototype.indexOf.call(NodeList, TargetNode);
        // If it moves forward, compensate for the removal
        if (CurIdx !== -1 && TargetIdx > CurIdx) { TargetIdx++; }
      end;
      if CurIdx=-1 then; // compiler honey

      // fetch reference node for insertBefore (or nil if append)
      if TargetIdx < NodeLen then
        RefNode := NodeList.item(TargetIdx)
      else
        RefNode := nil; // should not happen after the ++ above

      if Assigned(RefNode) then
        ChildContainer.insertBefore(TargetNode, RefNode)
      else
        ChildContainer.appendChild(TargetNode);

      C.Loaded;
    end;
  end
  else
  begin
    // no container yet -> just finalize direct children
    for i := 0 to ControlCount - 1 do
      Controls[i].Loaded;
  end;

  // ---------- 2. finalize non‑visual components ----------
  for i := 0 to ComponentCount - 1 do
    if (csLoading in Components[i].ComponentState) then
      Components[i].Loaded;

  // ---------- 3. handle alClient alignment ----------
  if Align = alClient then
    if Assigned(Parent) then
      SetBoundsInt(0, 0, Parent.FWidth, Parent.FHeight);

  // ---------- 4. finish up ----------
  Resize;
  UpdateElement;
end;
