Skip to main content

Try it Live

Run EventLog examples in the interactive playground

    Clone Behavior

    clone() creates deep copies of mutable fields:
    • Topics array - New array with same topic references
    • Data - New Uint8Array with copied bytes
    • Immutable fields - Shared references (address, hashes, numbers)
    import { EventLog } from 'tevm';
    
    const original = EventLog({
      address: contractAddress,
      topics: [topic0, topic1],
      data: new Uint8Array([1, 2, 3]),
      blockNumber: 100n,
    });
    
    const cloned = original.clone();
    
    // Topics array is independent
    cloned.topics.push(newTopic); // Error: topics is readonly
    
    // Data is independent
    cloned.data[0] = 99;
    console.log(original.data[0]); // 1 (unchanged)
    
    // Immutable fields are shared
    console.log(original.address === cloned.address); // true (same reference)
    console.log(original.blockNumber === cloned.blockNumber); // true
    

    Usage Patterns

    Cloning for Modification

    import { EventLog } from 'tevm';
    
    const log = EventLog({
      address: contractAddress,
      topics: [eventSignature],
      data: eventData,
      removed: false,
    });
    
    // Clone before modifying
    const modified = EventLog.clone(log);
    (modified as any).removed = true; // Modify clone
    
    console.log(log.removed); // false (original unchanged)
    console.log(modified.removed); // true
    

    Cloning Collection

    import { EventLog } from 'tevm';
    
    const logs = [log1, log2, log3];
    
    // Clone all logs
    const cloned = logs.map(log => log.clone());
    
    // Or using static method
    const cloned2 = logs.map(EventLog.clone);
    

    Preventing Mutation

    import { EventLog } from 'tevm';
    
    function processLog(log: EventLog) {
      // Clone to prevent external mutations
      const safeCopy = log.clone();
    
      // Modify safely
      // ... process safeCopy ...
    
      return result;
    }
    
    const original = EventLog({ /* ... */ });
    const result = processLog(original);
    // original is unchanged
    

    Performance

    Clone creates new arrays and copies bytes:
    • Topics array allocation - Small overhead (typically 1-4 elements)
    • Data copy - Proportional to data size (typically 0-1024 bytes)
    • Immutable fields - Zero overhead (shared references)
    For read-only operations, cloning is unnecessary. Only clone when modifications are needed.

    See Also