简体   繁体   中英

How to name an arrow function to make the name available in Chrome CPU profiler

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.

错误堆栈

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM