2

I am new to performance optimization, and while I recognize Node.js may not be the most beginner friendly place to start, it's the task at hand.

The observation: simple JSON API requests take in the order of hundreds of milliseconds on a staging server with no load and <10 users in the database. Particularly, the call to /api/get_user is taking ~300ms.

To execute this code:

exports.get_user = function(req, res) {
  return res.json(req.user)
}

(Note: we store our sessions in Redis)

The stack:

  • Node.js
  • Express
  • Redis
  • Mongo

Where do I start?

6
  • How are you initializing req.user? Commented Dec 20, 2013 at 2:02
  • 1
    There really isn't enough info here to guess what issue you are hitting. Commented Dec 20, 2013 at 3:53
  • Please specify where physically your services (node, redis, mongo) appear. Provide a larger piece of code which is actually executed. Also it is usually easy to identify the time taking operation by adding console.log(new Date()) in script which is executed. Commented Dec 21, 2013 at 0:06
  • @jibsales I should have mentioned I'm using Express / PassportJS (and mongoose). Here's passport.deserializeUser: jsfiddle.net/Y2vJS Commented Dec 21, 2013 at 1:31
  • @igorpavlov are you asking what order redis / mongo / appear in app.js / what other middleware the request traverses before hitting exports.get_user ? Commented Dec 21, 2013 at 2:05

2 Answers 2

1

Passing the –-nouse_idle_notification flag will tell V8 to ignore idle notification calls from Node, which are requests to V8 asking it to run GC immediately, as the Node process is currently idle. Because Node is aggressive with these calls (efficiency breeds clean slates), an excess of GC may slow down your application. Note that using this flag does not disable GC; GC simply runs less often. In the right circumstances this technique can increase performance.

Sign up to request clarification or add additional context in comments.

Comments

0

While it might be an overkill for this small scenario, you might want to consider profiling. I found the nodetime.com service quite useful.

3 Comments

I ran a transaction profile on /api/get_user on nodetime and got 12.9ms: nodetime screenshot. But the same request on chrome dev tools shows 607ms: dev tools screenshot. Since nodetime is just measuring how long it takes for node to generate the request, should I assume the difference is the time it takes for the request to travel over the open internet?
Yes that is a reasonable assumption. I don't know the details about your deployment topology. There might be latency on your line or simply you might be spending time in dns resolution. In addition you might want to measure a stream of continuous request discarding the first few, which tend to take longer due to loading time.
I discovered nginx's limit_req directive was too restrictive, and responsible for the majority of the delay. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.