Skip to content

Only update each-blocks if their value has changed #381

Closed
@Rich-Harris

Description

@Rich-Harris

The 'Each blocks' example in the REPL produces this code:

// ...	
update: function ( changed, root ) {
    var __tmp;

    var eachBlock_value = root.cats;
    
    for ( var i = 0; i < eachBlock_value.length; i += 1 ) {
        if ( !eachBlock_iterations[i] ) {
            eachBlock_iterations[i] = renderEachBlock( root, eachBlock_value, eachBlock_value[i], i, component );
            eachBlock_iterations[i].mount( eachBlock_anchor.parentNode, eachBlock_anchor );
        } else {
            eachBlock_iterations[i].update( changed, root, eachBlock_value, eachBlock_value[i], i );
        }
    }
    
    teardownEach( eachBlock_iterations, true, eachBlock_value.length );
    
    eachBlock_iterations.length = eachBlock_value.length;
},

If it were this instead...

// ...	
update: function ( changed, root ) {
    var __tmp;

    if ( 'cats' in changed ) {
        var eachBlock_value = root.cats;
        
        for ( var i = 0; i < eachBlock_value.length; i += 1 ) {
            if ( !eachBlock_iterations[i] ) {
                eachBlock_iterations[i] = renderEachBlock( root, eachBlock_value, eachBlock_value[i], i, component );
                eachBlock_iterations[i].mount( eachBlock_anchor.parentNode, eachBlock_anchor );
            } else {
                eachBlock_iterations[i].update( changed, root, eachBlock_value, eachBlock_value[i], i );
            }
        }
        
        teardownEach( eachBlock_iterations, true, eachBlock_value.length );
        
        eachBlock_iterations.length = eachBlock_value.length;
    }
},

...we'd prevent a lot of unnecessary work.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions