-
Notifications
You must be signed in to change notification settings - Fork 410
Labels
Description
[Enhancement] Replace JSON.parse(JSON.stringify()) with structuredClone() for object cloning
Summary
Replace the current LiteGraph.cloneObject() implementation that uses JSON.parse(JSON.stringify()) with the modern structuredClone() API to fix circular reference bugs and improve type preservation.
Problem
The current cloning implementation has several issues:
- Throws on circular references - causes crashes when objects have circular refs
- Poor type preservation - Dates become strings, RegExp becomes
{},undefinedbecomesnull - Missing modern API adoption -
structuredClone()has been available since 2022
Proposed Solution
Implement a hybrid approach with structuredClone() as primary method and JSON fallback:
cloneObject<T>(obj: T): T {
if (obj == null) return obj
if (typeof structuredClone !== 'undefined') {
try {
return structuredClone(obj)
} catch (error) {
console.warn('structuredClone failed, falling back to JSON method:', error)
}
}
return JSON.parse(JSON.stringify(obj))
}Benefits
- ✅ Fixes circular reference crashes
- ✅ Better type preservation (Dates, RegExp, undefined)
- ✅ Improved performance (native implementation)
- ✅ Backward compatibility (fallback for edge cases)
Impact Assessment
Risk Level: LOW ✅
- Compatibility: Already targeting ES2022/modern browsers
- Usage sites: 6 locations, all handle plain data objects
- Breaking changes: None expected (fallback maintains compatibility)
- Performance: Neutral to positive
Affected Files
src/LiteGraphGlobal.ts- Main implementationsrc/LGraphNode.ts- 4 usage sites (serialize, configure, clone)src/subgraph/subgraphUtils.ts- 1 usage site (multiClone)
Testing Requirements
- Circular reference handling
- Date/RegExp preservation
- undefined value handling
- Widget value compatibility
- Fallback behavior
- Performance regression testing
Implementation Steps
- Phase 1: Replace
cloneObject()with hybrid implementation - Phase 2: Add comprehensive tests
- Phase 3: Monitor for any behavioral changes in production
- Phase 4: Consider removing fallback after confidence period
References
- MDN structuredClone documentation
- Audit report:
STRUCTURED_CLONE_AUDIT.md - Browser support: Chrome 98+, Firefox 94+, Safari 15.4+, Node.js 17+
Priority
Medium - This fixes real bugs (circular references) and improves robustness, but doesn't block current functionality.
Acceptance Criteria:
-
LiteGraph.cloneObject()usesstructuredClone()when available - Maintains fallback to JSON method for compatibility
- All existing tests pass
- New tests added for circular references and type preservation
- No regressions in node serialization/deserialization
- Performance neutral or improved
┆Issue is synchronized with this Notion page by Unito