DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. JavaScript
  4. Beware the Performance Cost of async_hooks (Node 8)

Beware the Performance Cost of async_hooks (Node 8)

Let's look at the performance trade-offs of async_hooks to see if they're too good to be true.

Jakub Holý user avatar by
Jakub Holý
·
Nov. 06, 18 · Analysis
Like (1)
Save
Tweet
Share
8.04K Views

Join the DZone community and get the full member experience.

Join For Free

I was excited about async_hooks having finally landed in Node.js 8, as it would enable me to share important troubleshooting information with all code involved in handling a particular request. However, it turned out to have a terrible impact on our CPU usage (YMMV):

This was quite extreme and is likely related to the way how our application works and uses Promises. Do your own testing to measure the actual impact in your app.

However I am not the only one who has seen some performance hit from async_hooks — see here, in particular:

Here the results of running the Promise micro benchmarks with and without async_hooks enabled:

Image title

To confirm the impact of async_hook on our app, I have performed 3 performance tests:

CPU Usage Without async_hooks (Node 8)

It is difficult to see but the mean CPU usage is perhaps around 60% here.

CPU Usage With "no-op" async_hooks (Node 8)

Here the CPU jumped to 100%.

CPU Usage With "no-op" async_hooks (Node 11)

The same as above, but using Node 11 for comparison. I recorded it for just a few minutes but the CPU usage is still around 100%:

This is the relevant code:

const asyncHooks = require('async_hooks'); // Node 8.9+
const querystring = require('querystring');
const crypto = require("crypto");

const context = {};

function createHooks() {
    function init(asyncId, type, triggerId, resource) {
        // if (context[triggerId]) {
        //     context[asyncId] = context[triggerId];
        // }
    }

    function destroy(asyncId) {
        // delete context[asyncId];
    }

    const asyncHook = asyncHooks.createHook({ init, destroy });

    asyncHook.enable();
}

createHooks()


app application Comparison (grammar) Measure (physics) Testing Node.js Requests

Published at DZone with permission of Jakub Holý, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Distributed Stateful Edge Platforms
  • The Top 3 Challenges Facing Engineering Leaders Today—And How to Overcome Them
  • Apache Kafka vs. Memphis.dev
  • Memory Debugging: A Deep Level of Insight

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: