箭头函数是否像命名函数一样优化? [英] Are arrow functions optimized like named functions?

查看:160
本文介绍了箭头函数是否像命名函数一样优化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在观看一场NodeJS互动式演讲,而那个人说的是匿名功能是如何糟糕的原因之一是,如果他们没有名字,VM无法根据其使用频率来优化功能,因为它无名。 / p>

因此,如果调用具有名称的函数



random.Async('Blah ',函数randomFunc(){});



randomFunc 可以优化为其中函数如:



random.Async('Blah',function(cb){});



这不会被优化,因为它是匿名的,无名的。



所以我想知道箭头函数是否会这样做同样的事情,因为我不认为你可以命名箭头功能。





random.Async('Blah',(cb)=> {}); 优化?



编辑:寻找链接到这个家伙提到这个的谈话,将报告回来。 (这个讲座是从不久前开始的,它只是我记忆中的事情)



编辑找到视频: https://youtu.be/_0W_822Dijg?t=299

解决方案

注意,不完全确定这些是链接视频演示中讨论的模式比较。



在10000次迭代中,命名函数似乎在铬的V8实现时完成得最快。 箭头函数似乎以比匿名函数更短的时间返回结果。



在100000次迭代中,匿名函数在最短的时间内完成; 64.51ms 小于命名函数,而箭头函数花费 4902.01ms 完成比命名函数更多的时间。



  var len = Array.from({length:100000})//命名函数函数_named(){console.profile(named function ); console.time(命名函数);函数解析器(resolve,reject){resolve(named function)} function done(data){console.log(data)} function complete(){console.timeEnd(named function); console.profileEnd(); returnnamed function complete} function callback(){return new Promise(resolver).then(done)}返回Promise.all(len.map(callback))。then(complete); } //匿名函数函数_anonymous(){console.profile(anonymous function); console.time(匿名函数);返回Promise.all(len.map(function(){return new Promise(function(resolve,reject){resolve(anonymous function)})。then(function(data){console.log(data)})} ).then(function(){console.timeEnd(anonymous function); console.profileEnd();返回匿名函数完成}}} //箭头函数函数_arrow(){console.profile(arrow function ); console.time(箭头功能); return Promise.all(len.map(()=> {return new Promise((resolve,reject)=> resolve(arrow function))。then((data)=> {console.log(data }})}))。then(()=> {console.timeEnd(arrow function); console.profileEnd();返回arrow function complete})} _named()。then(_anonymous).then (_arrow) 



jsfiddle https://jsfiddle.net/oj87s38t/


I was watching a NodeJS Interactive talk and the guy speaking was saying how anonymous functions were bad one of the reasons being that if they have no name, the VM cannot optimize the function based on how frequently it's used because its nameless.

So if a function with a name is called

random.Async('Blah', function randomFunc() {});

randomFunc can be optimized as where a function like:

random.Async('Blah', function(cb) {});

This will not be optimized because it's anonymous, nameless.

So I was wondering if arrow functions would do the same thing because I don't think you can name arrow functions.

Will

random.Async('Blah', (cb) => {}); be optimized?

Edit: Looking for link to the talk where the guy mentions this, will report back. (This talk was from a while ago and its just something I remembered from it)

Edit Found the video: https://youtu.be/_0W_822Dijg?t=299

解决方案

Note, Not entirely certain that these are the pattern comparisons discussed at linked video presentation.

At 10000 iterations, named function appears to complete fastest at V8 implementation at chromium. Arrow function appeared to return results in less time than anonymous function.

At 100000 iterations anonymous function completed in briefest time; 64.51ms less than named function, while arrow function took 4902.01ms more time to complete than named function.

    var len = Array.from({
      length: 100000
    })

     // named function
    function _named() {

      console.profile("named function");
      console.time("named function");

      function resolver(resolve, reject) {
        resolve("named function")
      }

      function done(data) {
        console.log(data)
      }

      function complete() {
        console.timeEnd("named function");
        console.profileEnd();
        return "named function complete"
      }

      function callback() {
        return new Promise(resolver).then(done)
      }

      return Promise.all(len.map(callback)).then(complete);
    }

     // anonymous function
    function _anonymous() {
      console.profile("anonymous function");
      console.time("anonymous function");

      return Promise.all(len.map(function() {
          return new Promise(function(resolve, reject) {
              resolve("anonymous function")
            })
            .then(function(data) {
              console.log(data)
            })
        }))
        .then(function() {
          console.timeEnd("anonymous function");
          console.profileEnd();
          return "anonymous function complete"
        })
    }

     // arrow function
    function _arrow() {
      console.profile("arrow function");
      console.time("arrow function");

      return Promise.all(len.map(() => {
          return new Promise((resolve, reject) =>
              resolve("arrow function")
            )
            .then((data) => {
              console.log(data)
            })
        }))
        .then(() => {
          console.timeEnd("arrow function");
          console.profileEnd();
          return "arrow function complete"
        })
    }

    _named().then(_anonymous).then(_arrow)

jsfiddle https://jsfiddle.net/oj87s38t/

这篇关于箭头函数是否像命名函数一样优化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆