1
const result: Model[] = [];
array.forEach((element) => {
  if (condition1)
    result.push(structuredClone(element));

  if (condition2)
    result.push(structuredClone(element));
});

The object is nested:

export interface Model extends Dto {
  indentation?: number;
  previous?: Model;
  next?: Model;
}

export interface Dto {
  ...
  children?: Array<Dto>;
  contentChunks?: Array<ContentChunk>;
  id?: number;
  ...
}

If the size of array is small i works. But on big arrays it runs into

ERROR RangeError: Maximum call stack size exceeded

Suggestions to improve appreciated.

Update:

on changing structuredClone to JSON.parse(JSON.stringify

ERROR TypeError: Converting circular structure to JSON
--> starting at object with constructor 'Object'
|     property 'next' -> object with constructor 'Object'
--- property 'previous' closes the circle
Juri
  • 1,531
  • 2
  • 20
  • 43
  • How big the array should be to get the error? – P.S. Dec 27 '22 at 12:13
  • @P.S. 3925 Objects – Juri Dec 27 '22 at 12:37
  • i have tryed array.map - same issue – Juri Dec 27 '22 at 12:38
  • The error most probably is due an element item's structure. A simple array iteration regardless of the array's length does not create a call-stack-size range-error; code execution just might take its time. What does the structure of an element-item look like? – Peter Seliger Dec 27 '22 at 13:40
  • @PeterSeliger it looks like this export interface DocumentFragmentModel extends FragmentDto { indentation?: number; previousFragment?: DocumentFragmentModel; nextFragment?: DocumentFragmentModel; } export interface FragmentDto { ... children?: Array; contentChunks?: Array; ... } – Juri Dec 27 '22 at 13:47
  • @Juri ... The OP might consider editing the Q. and providing the structure in a formatted way preferably as pure JS. `structuredClone` after all deals with JS objects. TypeScript annotation might just distract one from debugging. – Peter Seliger Dec 27 '22 at 13:52
  • the error doesn't happen in firefox but in chome – Juri Dec 27 '22 at 14:19
  • @PeterSeliger appreciated, updated the Q – Juri Dec 27 '22 at 14:33
  • That seems to be an error while cloning an element. It probably has some circular structure. – derpirscher Dec 27 '22 at 14:34
  • So it works for 3924 elements but throws at the 3925? What happens if you remove `forEach` and just call `structuredClone(array[3294])` directly? – derpirscher Dec 27 '22 at 14:38
  • @derpirscher i have updated the question, object structure seems to be the issue. Still dont understand, why it works on some smaller arrays – Juri Dec 27 '22 at 14:57
  • It probably doesn't have anything to do with the size of the array but that the invalid object is at a later index. If the invalid object was at index 3 it would also fail for an array of size 4 – derpirscher Dec 27 '22 at 16:08
  • @derpirscher ... `structuredClone` is designed for handling circular references. – Peter Seliger Dec 27 '22 at 16:42
  • @Juri ... emulating a deep clone with a JSON based approach ignores any non valid JSON type and fails at circular references. Thus with the OP's edited question it explains the failure on the latter. [But `structuredClone` is designed for handling circular references](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm). Which now points to having a deep dive into the very object the [`structuredClone`](https://developer.mozilla.org/en-US/docs/Web/API/structuredClone) fails at. What does this object look like? – Peter Seliger Dec 27 '22 at 16:46
  • @PeterSeliger Might be, but it seems to fail at that particular object. Otherwise, why would it throw *Maximum Call Stack exceeded* exception ... – derpirscher Dec 27 '22 at 17:03
  • @derpirscher ... On the other hand the OP claims that it fails in chrome but not in firefox. The OP even could accidentally have discovered an implementation bug. But one can not tell until one inspects the JavasSript object (TypeScript does not help) and does run test cases with it. – Peter Seliger Dec 27 '22 at 17:09
  • Might be, That's why I asked what happens if OP calls `structuredClone` directly on the (seemingly) failing object. Because I still think it doesn't have anything to do with the size of the array, but (at least) one object breaking the clone method ... – derpirscher Dec 27 '22 at 17:15
  • safari on mac is also running without issues. Error message on mac on chrome is also different – Juri Dec 28 '22 at 09:59
  • if i add console.log just before structuredClone it is logged only once. So the error happens on the first iteration – Juri Dec 28 '22 at 10:04
  • @derpirscher If i set next, previous and children = null before structuredClone() working like a charm – Juri Dec 28 '22 at 10:31
  • @Juri well, obviously, if you don't have connections to any other object you won't get into a circular structure. This supports the theory that it's an issue with your data-object in combination with the implementation of `strucuredClone` It may be a bug in chrome. – derpirscher Dec 28 '22 at 14:08
  • Can you provide a [mre] in a code snippet? – jsejcksn Dec 28 '22 at 14:38

0 Answers0