0

This is a variation of How do I write a named arrow function in ES2015? question, though the specific aim is to name the function object in a way that would make it available for Chrome CPU profiler.

I have tried the following:

let unnamed;

unnamed = () => {

};

Object.defineProperty(unnamed, 'name', {
    value: 'XXX'
});

Object.defineProperty(unnamed.constructor, 'name', {
    value: 'YYY'
});

Object.defineProperty(unnamed, 'displayName', {
    value: 'ZZZ'
});

console.log('unnamed.name', unnamed.name);
console.log('unnamed.constructor.name', unnamed.constructor.name);
console.log('unnamed.displayName', unnamed.displayName);

export default unnamed;

However, whatever I do, CPU profiler is showing it as (anonymous function).

Interestingly, if you throw an error, the stack trace uses displayName property.

Error stack

Community
  • 1
  • 1
Gajus
  • 69,002
  • 70
  • 275
  • 438
  • Found a relevant thread on Chromium issue tracker, https://code.google.com/p/chromium/issues/detail?can=2&start=0&num=100&q=&colspec=ID%20Pri%20M%20Stars%20ReleaseBlock%20Cr%20Status%20Owner%20Summary%20OS%20Modified&groupby=&sort=&id=17356 – Gajus Feb 02 '16 at 14:49

1 Answers1

1

CPU profiler doesn't use displayName or Function.name. There is another issue for this: https://code.google.com/p/chromium/issues/detail?id=559532

For stack trace function name resolution algo use first defined property from list: displayName, Function.name, V8 function inferred name.