Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
269 views
Welcome To Ask or Share your Answers For Others

1 Answer

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...