问题
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
回答1:
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/
回答2:
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.
来源:https://stackoverflow.com/questions/36732782/are-arrow-functions-optimized-like-named-functions