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
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/