• js性能-对象查找值与迭代查找值性能对比


    前言

    公司的一次cr,了解到对象查找值比迭代查找值效率高,因此整了个数据对比的界面

    结果

    在这里插入图片描述
    说明:

    1. Object:代表对象查找值
    2. Iteration:表示迭代查找值

    源码

    DOCTYPE html>
    <html>
      <head>
        <meta charset="utf-8" />
        <title>对象和循环对比title>
        
        
        <script src="https://gw.alipayobjects.com/os/antv/pkg/_antv.data-set-0.10.1/dist/data-set.min.js">script>
        <script src="https://gw.alipayobjects.com/os/lib/antv/g2/3.4.10/dist/g2.min.js">script>
      head>
      <style>
        .c-wrap {
          display: flex;
          flex-direction: row;
        }
        .code-wrap {
        }
      style>
      <body>
        <div>数据量(x)与消耗时间(y)对比div>
        
        <div class="c-wrap">
          <div id="mountNode">div>
          <div class="code-wrap">
            <div>
              <pre>  <code>
                    function funM1(apiDataList, localDataList) { // const start = new
                    Date().getTime(); const start = window.performance.now(); //
                    构造成对象 const apiDataObj = apiDataList.reduce((pre, cur) => { if
                    (!(cur.id in pre)) { pre[cur.id] = cur.price; } return pre; }, {});
                    const newList1 = localDataList.map((item) => { const apiDataObjItem
                    = apiDataObj[item.id]; if (apiDataObjItem) { item.price =
                    apiDataObjItem.price; } return item; }); // const end = new
                    Date().getTime(); const end = window.performance.now(); const
                    duration = end - start; return duration; }
                  code>pre>
            div>
            <div>
              <pre>  <code>
                    function funM2(apiDataList, localDataList) { // const start = new
                    Date().getTime(); const start = window.performance.now(); const
                    newList2 = localDataList.map((item) => { const tmpItem =
                    apiDataList.find((itemIn) => itemIn.id === item.id); if (tmpItem) {
                    item.price = tmpItem.price; } return item; }); // const end = new
                    Date().getTime(); const end = window.performance.now(); const
                    duration = end - start; return duration; }
                  code>pre>
            div>
          div>
        div>
    
        <script>
          /**
           * tips1:
           * genNumsLevels(level, step)
           * 可变更level 查看更多级别数据的运行情况
           *
           *
           * tips2:
           * 计时方法: window.performance.now(); 或者  new Date().getTime()
           **/
    
          //   数据准备 公共数据
          let showData = [];
          const numsLevels = genNumsLevels(10, 2000);
          numsLevels.map((item) => {
            const apiDataList = genApiDataList(item);
            const localDataList = genLocalDataList(item);
            const timeM1 = funM1(apiDataList, localDataList);
            const timeM2 = funM2(apiDataList, localDataList);
            showData.push({
              dataLevel: item,
              Object: timeM1,
              Iteration: timeM2,
            });
          });
    
          /**
           * . 算法1:使用对象寻找值 start
           **/
          function funM1(apiDataList, localDataList) {
            const start = new Date().getTime();
            // const start = window.performance.now();
            // 构造成对象
            const apiDataObj = apiDataList.reduce((pre, cur) => {
              if (!(cur.id in pre)) {
                pre[cur.id] = cur.price;
              }
              return pre;
            }, {});
    
            const newList1 = localDataList.map((item) => {
              const apiDataObjItem = apiDataObj[item.id];
              if (apiDataObjItem) {
                item.price = apiDataObjItem.price;
              }
              return item;
            });
            const end = new Date().getTime();
            // const end = window.performance.now();
            const duration = end - start;
            return duration;
          }
    
          /**
           * . 算法1:使用对象寻找值 end
           **/
    
          /**
           * . 算法2:使用循环寻找值 start
           **/
          function funM2(apiDataList, localDataList) {
            const start = new Date().getTime();
            // const start = window.performance.now();
            const newList2 = localDataList.map((item) => {
              const tmpItem = apiDataList.find((itemIn) => itemIn.id === item.id);
              if (tmpItem) {
                item.price = tmpItem.price;
              }
              return item;
            });
            const end = new Date().getTime();
            // const end = window.performance.now();
            const duration = end - start;
            return duration;
          }
    
          /**
           * . 算法2:使用循环寻找值 end
           **/
    
          // 构造数据量级
          function genNumsLevels(level, step) {
            const list = [];
            for (let i = 1; i <= level; i++) {
              list.push(2000 * i - step);
            }
            return list;
          }
    
          // 生成接口来的数据
          function genApiDataList(numsLevel) {
            const tmpList = [];
            for (let i = 0; i < numsLevel; i++) {
              tmpList.push({
                id: i + 1,
                price: i + 1,
              });
            }
            return tmpList;
          }
    
          // 生成本地已有的数据
          function genLocalDataList(numsLevel) {
            const tmpList = [];
            for (let i = 0; i < numsLevel; i++) {
              tmpList.push({
                id: i + 1,
                name: `名称${i + 1}`,
                price: i + 2.1,
              });
            }
            return tmpList;
          }
    
          let ds = new DataSet();
          let dv = ds.createView().source(showData);
          // fold 方式完成了行列转换,如果不想使用 DataSet 直接手工转换数据即可
          dv.transform({
            type: "fold",
            fields: ["Object", "Iteration"], // 展开字段集
            key: "city", // key字段
            value: "temperature", // value字段
          });
          let chart = new G2.Chart({
            container: "mountNode",
            // forceFit: true,
            width: 800, // 指定图表宽度
            height: 500, // 指定图表高度
          });
          chart.source(dv, {
            dataLevel: {
              range: [0, 1],
            },
          });
          chart.tooltip({
            crosshairs: {
              type: "line",
            },
          });
          chart.axis("temperature", {
            label: {
              formatter: function formatter(val) {
                return val + "ms";
              },
            },
          });
          chart
            .line()
            .position("dataLevel*temperature")
            .color("city")
            .shape("smooth");
          chart
            .point()
            .position("dataLevel*temperature")
            .color("city")
            .size(4)
            .shape("circle")
            .style({
              stroke: "#fff",
              lineWidth: 1,
            });
          chart.render();
        script>
      body>
    html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216

    结尾

    源码可以直接跑,图用的antv的cdn。over

  • 相关阅读:
    数学——图形题
    【数据结构(四)】栈(1)
    如果一定要在C++和JAVA中选择,是C++还是java?
    前端---ES5知识点小梳理二
    webrtc 生成unpack_aecdump工具
    MMDetection3D简单教程:模型定义、注册与搭建
    Spring Boot项目中JPA操作视图会改变原表吗?
    冯唐 成事心法
    Python集合详细教程
    阿里P8资深架构师耗时半年整理21年Java工程师成神之路
  • 原文地址:https://blog.csdn.net/qq_40020447/article/details/126381778