javascript/typescript lodash.cloneDeep: circularity in archetype data leads to "call stack size exceeded"

255 Views Asked by At

(Posted as an archetype issue)

[email protected], using [email protected], sometimes results in "call stack size exceeded", presumably due to circular references in the data, specifically encountered in:

  • archetype/src/type.js - Type constructor method
  • archetype/src/index.js - Archetype constructor and visitObject methods

So, first questions: which uses of archetype are circular (and how detected), and can the circularities be avoided or addressed?

As for possible workarounds/solutions, which of the many deepClone alternatives would be appropriate?

One possible solution - rfdc - addresses circular structures, albeit at a %25 performance hit:

const clone = require('rfdc')({circles: true})
...
b = clone(a)

The key question its whether circularities can be eliminated, whether an across-the-board %25 performance reduction can be tolerated, or whether circularity can be detected, to determine which flavor of deep cloning should be applied.

Some (problematic) workarounds:

  1. replace lodash.cloneDeep with clone, in each file:
  • replace const cloneDeep = require('lodash.clonedeep'); with const clone = require('clone');
  • replace references to _.cloneDeep(...) with references to clone(...)
  1. replace lodash.cloneDeep with fclone, as discussed here.

The problem with each is that circular values are replaced with a string: '[Circular]', presumably problematic when dealing with types.

Ultimately, this needs to be fixed in archetype, as that's where reliance on lodash.deepClone breaks down.

0

There are 0 best solutions below