Are arrow functions optimized like named functions?

前端 未结 2 1344
清酒与你
清酒与你 2020-12-11 23:54

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 o

相关标签:
2条回答
  • 2020-12-12 00:29

    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/

    0 讨论(0)
  • 2020-12-12 00:48

    I believe what Matteo was referring to were functions that are called very few times (e.g. a callback that is only called once) and the v8 optimizations have nothing to do with whether the function is actually anonymous or not.

    Also if you continue to watch, he mentions using a module called reusify that basically provides a pool of functions. By using this, it means you can get a function from the pool that may already be optimized, meaning it could execute faster than a typical one-time use callback. However not all use cases may be able to use something like this.

    0 讨论(0)
提交回复
热议问题