如何避免在`map`回调中进行多次计算?

时间:2018-12-26 12:32:54

标签: javascript arrays callback closures

我在“单线”之后写了一个用于归一化向量的向量(这里用通常的数组表示),这意味着将每个条目除以向量的欧几里得范数。

    <table id="table-1" class="table table-hover">
        <thead>
          <tr>
            <th>test</th>
            <th>test</th>
            <th>test</th>
            <th>test</th>
            <th>test</th>
            <th>test</th>
            <th>test</th>
            <th>test</th>
            <th>test</th>
            <th>test</th>
            <th>test</th>
            <th>test</th>
            <th>test</th>
          </tr>
        </thead>
    <body>
          <div class="content">
            <table class="table table-striped">
         <tbody>
    <tr class="bordered">
    <td align="center">test </td>
    <td align="center">test </td>
    <td align="center"> test</td>
    <td align="center"> test </td>
    <td align="center" data-toggle="modal"
     id="myBtn"> 
    test </td>
    <td align="center"> test</td>
    <td align="center"> 
     test</td>
    <td align="center"> test </td>
    <td align="center"> test </td>
    <td align="center"> test  </td>
    <td>     
         test
       </td>
    <td>   
    test
          </td>
        <td> 
        test
    </td>
    </tr>
    <tr class="bordered">
    <td align="center">test </td>
    <td align="center">test </td>
    <td align="center"> test</td>
    <td align="center"> test </td>
    <td align="center" data-toggle="modal"
     id="myBtn"> 
    test </td>
    <td align="center"> test</td>
    <td align="center"> 
     test</td>
    <td align="center"> test </td>
    <td align="center"> test </td>
    <td align="center"> test  </td>
    <td>     
         test
       </td>
    <td>   
    test
          </td>
        <td> 
        test
    </td>
    </tr>
    <tr class="bordered">
    <td align="center">test </td>
    <td align="center">test </td>
    <td align="center"> test</td>
    <td align="center"> test </td>
    <td align="center" data-toggle="modal"
     id="myBtn"> 
    test </td>
    <td align="center"> test</td>
    <td align="center"> 
     test</td>
    <td align="center"> test </td>
    <td align="center"> test </td>
    <td align="center"> test  </td>
    <td>     
         test
       </td>
    <td>   
    test
          </td>
        <td> 
        test
    </td>
    </tr>
    <tr class="bordered">
    <td align="center">test </td>
    <td align="center">test </td>
    <td align="center"> test</td>
    <td align="center"> test </td>
    <td align="center" data-toggle="modal"
     id="myBtn"> 
    test </td>
    <td align="center"> test</td>
    <td align="center"> 
     test</td>
    <td align="center"> test </td>
    <td align="center"> test </td>
    <td align="center"> test  </td>
    <td>     
         test
       </td>
    <td>   
    test
          </td>
        <td> 
        test
    </td>
    </tr>
    <tr class="bordered">
    <td align="center">test </td>
    <td align="center">test </td>
    <td align="center"> test</td>
    <td align="center"> test </td>
    <td align="center" data-toggle="modal"
     id="myBtn"> 
    test </td>
    <td align="center"> test</td>
    <td align="center"> 
     test</td>
    <td align="center"> test </td>
    <td align="center"> test </td>
    <td align="center"> test  </td>
    <td>     
         test
       </td>
    <td>   
    test
          </td>
        <td> 
        test
    </td>
    </tr>
    <tr class="bordered">
    <td align="center">test </td>
    <td align="center">test </td>
    <td align="center"> test</td>
    <td align="center"> test </td>
    <td align="center" data-toggle="modal"
     id="myBtn"> 
    test </td>
    <td align="center"> test</td>
    <td align="center"> 
     test</td>
    <td align="center"> test </td>
    <td align="center"> test </td>
    <td align="center"> test  </td>
    <td>     
         test
       </td>
    <td>   
    test
          </td>
        <td> 
        test
    </td>
    </tr><tr class="bordered">
    <td align="center">test </td>
    <td align="center">test </td>
    <td align="center"> test</td>
    <td align="center"> test </td>
    <td align="center" data-toggle="modal"
     id="myBtn"> 
    test </td>
    <td align="center"> test</td>
    <td align="center"> 
     test</td>
    <td align="center"> test </td>
    <td align="center"> test </td>
    <td align="center"> test  </td>
    <td>     
         test
       </td>
    <td>   
    test
          </td>
        <td> 
        test
    </td>
    </tr>
    <tr class="bordered">
    <td align="center">test </td>
    <td align="center">test </td>
    <td align="center"> test</td>
    <td align="center"> test </td>
    <td align="center" data-toggle="modal"
     id="myBtn"> 
    test </td>
    <td align="center"> test</td>
    <td align="center"> 
     test</td>
    <td align="center"> test </td>
    <td align="center"> test </td>
    <td align="center"> test  </td>
    <td>     
         test
       </td>
    <td>   
    test
          </td>
        <td> 
        test
    </td>
    </tr>
    <tr class="bordered">
    <td align="center">test </td>
    <td align="center">test </td>
    <td align="center"> test</td>
    <td align="center"> test </td>
    <td align="center" data-toggle="modal"
     id="myBtn"> 
    test </td>
    <td align="center"> test</td>
    <td align="center"> 
     test</td>
    <td align="center"> test </td>
    <td align="center"> test </td>
    <td align="center"> test  </td>
    <td>     
         test
       </td>
    <td>   
    test
          </td>
        <td> 
        test
    </td>
    </tr>
    <tr class="bordered">
    <td align="center">test </td>
    <td align="center">test </td>
    <td align="center"> test</td>
    <td align="center"> test </td>
    <td align="center" data-toggle="modal"
     id="myBtn"> 
    test </td>
    <td align="center"> test</td>
    <td align="center"> 
     test</td>
    <td align="center"> test </td>
    <td align="center"> test </td>
    <td align="center"> test  </td>
    <td>     
         test
       </td>
    <td>   
    test
          </td>
        <td> 
        test
    </td>
    </tr>
    <tr class="bordered">
    <td align="center">test </td>
    <td align="center">test </td>
    <td align="center"> test</td>
    <td align="center"> test </td>
    <td align="center" data-toggle="modal"
     id="myBtn"> 
    test </td>
    <td align="center"> test</td>
    <td align="center"> 
     test</td>
    <td align="center"> test </td>
    <td align="center"> test </td>
    <td align="center"> test  </td>
    <td>     
         test
       </td>
    <td>   
    test
          </td>
        <td> 
        test
    </td>
    </tr>
    <tr class="bordered">
    <td align="center">test </td>
    <td align="center">test </td>
    <td align="center"> test</td>
    <td align="center"> test </td>
    <td align="center" data-toggle="modal"
     id="myBtn"> 
    test </td>
    <td align="center"> test</td>
    <td align="center"> 
     test</td>
    <td align="center"> test </td>
    <td align="center"> test </td>
    <td align="center"> test  </td>
    <td>     
         test
       </td>
    <td>   
    test
          </td>
        <td> 
        test
    </td>
    </tr>
    <tr class="bordered">
    <td align="center">test </td>
    <td align="center">test </td>
    <td align="center"> test</td>
    <td align="center"> test </td>
    <td align="center" data-toggle="modal"
     id="myBtn"> 
    test </td>
    <td align="center"> test</td>
    <td align="center"> 
     test</td>
    <td align="center"> test </td>
    <td align="center"> test </td>
    <td align="center"> test  </td>
    <td>     
         test
       </td>
    <td>   
    test
          </td>
        <td> 
        test
    </td>
    </tr><tr class="bordered">
    <td align="center">test </td>
    <td align="center">test </td>
    <td align="center"> test</td>
    <td align="center"> test </td>
    <td align="center" data-toggle="modal"
     id="myBtn"> 
    test </td>
    <td align="center"> test</td>
    <td align="center"> 
     test</td>
    <td align="center"> test </td>
    <td align="center"> test </td>
    <td align="center"> test  </td>
    <td>     
         test
       </td>
    <td>   
    test
          </td>
        <td> 
        test
    </td>
    </tr>
    <tr class="bordered">
    <td align="center">test </td>
    <td align="center">test </td>
    <td align="center"> test</td>
    <td align="center"> test </td>
    <td align="center" data-toggle="modal"
     id="myBtn"> 
    test </td>
    <td align="center"> test</td>
    <td align="center"> 
     test</td>
    <td align="center"> test </td>
    <td align="center"> test </td>
    <td align="center"> test  </td>
    <td>     
         test
       </td>
    <td>   
    test
          </td>
        <td> 
        test
    </td>
    </tr>
    </tbody>
    </table>
    <table id="header-fixed"></table>
var tableOffset = $("#table-1").offset().top;

var $header = $("#table-1 > thead").clone();

var $fixedHeader = $("#header-fixed").append($header);



$(window).bind("scroll", function() {

    var offset = $(this).scrollTop();



    if (offset >= tableOffset && $fixedHeader.is(":hidden")) {

        $fixedHeader.show();

    }

    else if (offset < tableOffset) {

        $fixedHeader.hide();

    }

});

不幸的是,它不是非常有效,因为该规范是针对每个条目一遍又一遍地计算的。我们能否以避免重复评估的方式修改此“单线”?

编辑:我发现的一种方法-并不是特别优雅且不易读-需要在最外层的lambda上添加另一个输入参数,并将其用作规范的输入:

normalize = v => v.map(x => x/Math.sqrt(v.map(x => x * x).reduce((a, b) => a + b)))

1 个答案:

答案 0 :(得分:0)

只是不要尝试变得太聪明...

const normalize = v => {
    const norm = Math.sqrt(v.map(x => x * x).reduce((a, b) => a + b)));
    return v.map(x => x / norm);
};