美文网首页
抓耳挠腮

抓耳挠腮

作者: 坠入莱茵河 | 来源:发表于2017-11-29 11:44 被阅读0次

感觉已经解决的时候 经过多次以及大数据量测试 发觉各个方式的耗时都没有不同 就连一开始@二石兄
抛出的假象,为什么遍历次数多反而耗时少?的现象都不见了 ? 真是令人抓耳挠腮 返回重新看这个问题 重新测试 重新.... 南辕北辙的故事

看了 二师兄@假象,为什么遍历次数多反而耗时少?抛出的问题
惊讶的发现 还真是!

曾想起在之前的学习过程中,读到过 ES6 的 class 是一个基于原型链的一个语法糖

尝试使用原型链的方式定义这个函数


// es6 Class
class ParseArray {
  constructor(inputArray) {
      this.inputArray = inputArray
      this.outputArray = [{
          "sub_data": []
      }]
      this.loopLength = 0
  }
  loop(pId, items) {
      this.inputArray.forEach((item, idx) => {
          this.loopLength++
              if (item[1] === pId) {
                  items.push({
                      main_data: item.slice(2),
                      sub_data: []
                  })
                  delete this.inputArray[idx] // 录入后删掉该条数据 可降低循环次数
                  this.loop(item[0], items[items.length - 1].sub_data)
              }
      })
  }
}

console.time("es6class")
let val = new ParseArray(array.slice())
val.loop('', val.outputArray[0].sub_data)
// console.log(JSON.stringify(val.outputArray))
console.log('es6class', val.loopLength) // => 26
console.timeEnd("es6class")     // => loop: 3.64501953125ms


// es5 prototype


function ParseArrayEs5 () {
  this.loopLength = 0
  this.outputArray = []
  this.array2 = JSON.parse(JSON.stringify(array))
}

ParseArrayEs5.prototype.loop = function(pId, items) { // 注意 箭头表达式的this指向 与function的this指向不一致 function this指向function 箭头表达式this指向函数的上一级
  this.array2.forEach((item, idx) => {
    this.loopLength++
    if (item[1] === pId) {
        items.push({
            main_data: item.slice(2),
            sub_data: []
        })
        delete this.array2[idx] // 录入后删掉该条数据 可降低循环次数
        this.loop(item[0], items[items.length - 1].sub_data)
    }
  })
}

console.time("es5")

let outputArray666 = new ParseArrayEs5()
outputArray666.loop('', outputArray666.outputArray)
console.log('es5', outputArray666.loopLength) // => 26
console.timeEnd("es5")     // => loop: 0.2919921875ms

Class的运算效率居然差了这么多

Google 查了一下 常见的分析一般都是 new 一个 Class 要比 new 一个 工厂函数效率要低的多
测试用例也基本都是循环new Class
可是本例中只new了一次 照理说 new Class 的损耗只有第一次??

查看了其他文章中提及 Babel 会将ES6 代码转换为ES5 代码
这就去转换了一下

babel转码.png

仔细查看了一下 和别人文章中讲的一致 创建类时要比原型链复杂许多 具体可以搜索相关文章
但与本例情况不同 本例中Class 只new了一次 而 递归的部分 才是大头 把注意力放在 _createdClass 下的 value function loop 中
细看后发现

value: function loop(pId, items) {
            var _this = this; // 绑定this 
            this.inputArray.forEach(function (item, idx) {
                _this.loopLength++; // 如果没有临时变量来绑定this, 将会无法获取loopLength

这是function常用的绑定作用域的方式 用一个临时变量来解决 function 作用域 不一致的问题
但声明临时变量和绑定this是一项多余的动作
修改一下转码后的代码后

_createClass(ParseArray, [{
        key: "loop",
        value: function loop(pId, items) {
            // 删除 var _this = this;
            this.inputArray.forEach((item, idx) => { // 原 this.inputArray.forEach(function (item, idx) {
                this.loopLength++; // 由于修改了作用域 this 生效
                if (item[1] === pId) {
                    items.push({
                        main_data: item.slice(2),
                        sub_data: []
                    });
                    delete this.inputArray[idx]; // // 由于修改了作用域 this 生效
                    this.loop(item[0], items[items.length - 1].sub_data);
                }
            });
        }
    }]);


相关文章

  • 抓耳挠腮

    正在为北大荒文化编辑10000字左右的章节,抓破头皮找资料,眼疲劳啊

  • 抓耳挠腮

    感觉已经解决的时候 经过多次以及大数据量测试 发觉各个方式的耗时都没有不同 就连一开始@二石兄抛出的假象,为什么遍...

  • 抓耳挠腮

    如果不是刚打了耳洞一碰到耳垂那儿就痛,都不知道原来自己想不出办法的时候真的会习惯性“抓耳挠腮”,古人诚不欺也。 b...

  • 抓耳挠腮

    时间真的不等人啊,一个上午,马上又要过去了,回头看看自己做了什么????? 看了一上午的视频,没有玩游戏,一直看外...

  • 抓耳挠腮

    2020年3月26日 星期四 雨 今天下雨降温了,天阴沉的很,早上起床洗漱完,你坐在餐桌前故意撅着嘴说:“烦!” ...

  • 抓耳挠腮

    因为今天要跟客户过结项时用的汇报材料,所以昨晚做梦说,原来服务一个企业办学的学校的项目对接人要我上一节课,然后就讲...

  • “抓耳挠腮”好处多

    人在下意识中做的小动作,其实都是大有深意的。比如这个“抓耳挠腮”。人在焦急忙乱的时候,为什么要抓耳挠腮?因为这样做...

  • 考场作文不再抓耳挠腮

    有一个人他叫小明(小明:我跟你什么仇什么怨?!又拿我说事!),他不会写作文,他想知道怎么绞尽脑汁写满作文纸,请问他...

  • 没有灵感,抓耳挠腮

    写作这件事,是真的需要灵感的! 昨天还在兴致勃勃地给自己点赞,今天就想让自己罚站。(对,为了押韵) 工作完一天,逛...

  • 轻而易举的开始,抓耳挠腮地坚持

    我没有持续写作的习惯,却喜欢偶尔心血来潮,写上只言片语给自己,过些日子又可以回味当时的心情,也算是记录自己...

网友评论

      本文标题:抓耳挠腮

      本文链接:https://www.haomeiwen.com/subject/qigebxtx.html