javascript - Are arrow functions optimized like named functions? -
i watching nodejs interactive talk , guy speaking saying how anonymous functions bad 1 of reasons being if have no name, vm cannot optimize function based on how it's used because nameless.
so if function name called
random.async('blah', function randomfunc() {});
randomfunc
can optimized function like:
random.async('blah', function(cb) {});
this not optimized because it's anonymous, nameless.
so wondering if arrow functions same thing because don't think can name arrow functions.
will
random.async('blah', (cb) => {});
optimized?
edit: looking link talk guy mentions this, report back. (this talk while ago , remembered it)
edit found video: https://youtu.be/_0w_822dijg?t=299
note, not entirely these pattern comparisons discussed @ linked video presentation.
at 10000 iterations, named function appears complete fastest @ v8 implementation @ chromium. arrow function
appeared return results in less time anonymous function.
at 100000 iterations anonymous function completed in briefest time; 64.51ms
less named function, while arrow function took 4902.01ms
more time complete 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/
Comments
Post a Comment