Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
281 views
in Technique[技术] by (71.8m points)

javascript - Are arrow functions optimized like named functions?

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

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

1 Reply

0 votes
by (71.8m points)

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...