We are no longer accepting contributions to Documentation. Please see our post on meta.

Node.js

Node.js Performance All Versions

v0.1
v0.2
v0.3
v0.4
v0.5
v0.6
v0.7
v0.8
v0.9
v0.10
v0.11
io.js v1.0
io.js v1.1
io.js v1.2
io.js v1.3
io.js v1.4
io.js v1.5
io.js v1.6
io.js v1.7
io.js v1.8
io.js v2.0
io.js v2.1
io.js v2.2
io.js v2.3
io.js v2.4
io.js v2.5
io.js v3.0
io.js v3.1
io.js v3.2
io.js v3.3
v4.0
v4.1
v4.2
v5.0
v5.1
v5.2
v5.3
v5.4
v5.5
v0.12
v4.3
v5.6
v5.7
v4.4
v5.8
v5.9
v5.10
v5.10.1
v5.11.0
v6.0.0
v5.11.1
v6.1.0
v6.2.0
v6.2.1
v6.2.2
v5.12.0
v6.3.0
v6.3.1
v6.4.0
v6.5.0
v6.6.0
v6.7.0
v6.8.0
v6.8.1
v6.9.0
v6.9.1
v7.0.0
v7.1.0
v7.2.0
v6.9.2
v7.2.1
v7.3.0
v7.4.0
v6.9.3
v6.9.4
v6.9.5
v7.5.0
v6.10.0
v7.6.0
v7.7.0
v7.7.1
v7.7.2
v7.7.3
v6.10.1
v7.7.4
v7.8.0
v6.10.2
v7.9.0
v6.10.3
v7.10.0
v8.0.0
v6.11.0
v8.1.0
v8.1.1
v8.1.2
v8.1.3
v8.1.4
v8.2.0
v8.2.1

This draft deletes the entire topic.

Examples

  • 4
    const http = require('http')
    const fs   = require('fs')
    const zlib = require('zlib')
    
    http.createServer((request, response) => {
      const stream          = fs.createReadStream('index.html')
      const acceptsEncoding = request.headers['accept-encoding']
    
      let encoder = {
        hasEncoder     : false,
        contentEncoding: {},
        createEncoder  : () => throw 'There is no encoder'
      }
    
      if (!acceptsEncoding) {
        acceptsEncoding = ''
      }
    
      if (acceptsEncoding.match(/\bdeflate\b/)) {
        encoder = {
          hasEncoder     : true,
          contentEncoding: { 'content-encoding': 'deflate' },
          createEncoder  : zlib.createDeflate
        }
      } else if (acceptsEncoding.match(/\bgzip\b/)) {
        encoder = {
          hasEncoder     : true,
          contentEncoding: { 'content-encoding': 'gzip' },
          createEncoder  : zlib.createGzip
        }
      }
    
      response.writeHead(200, encoder.contentEncoding)
    
      if (encoder.hasEncoder) {
        stream = stream.pipe(encoder.createEncoder())
      }
    
      stream.pipe(response)
    
    }).listen(1337)
    
  • 2

    Blocking Operation Example

    let loop = (i, max) => {
      while (i < max) i++
      return i
    }
    
    // This operation will block Node.js
    // Because, it's CPU-bound
    // You should be careful about this kind of code
    loop(0, 1e+12)
    

    Non-Blocking IO Operation Example

    let i = 0
    
    const step = max => {
      while (i < max) i++
      console.log('i = %d', i)
    }
    
    const tick = max => process.nextTick(step, max)
    
    // this will postpone tick run step's while-loop to event loop cycles
    // any other IO-bound operation (like filesystem reading) can take place
    // in parallel
    tick(1e+6)
    tick(1e+7)
    console.log('this will output before all of tick operations. i = %d', i)
    console.log('because tick operations will be postponed')
    tick(1e+8)
    

    event loop diagram

    In simpler terms, Event Loop is a single-threaded queue mechanism which executes your CPU-bound code until end of its execution and IO-bound code in a non-blocking fashion.

    However, Node.js under the carpet uses multi-threading for some of its operations through libuv Library.

    Performance Considerations

    • Non-blocking operations will not block the queue and will not effect the performance of the loop.
    • However, CPU-bound operations will block the queue, so you should be careful not to do CPU-bound operations in your Node.js code.

    Node.js non-blocks IO because it offloads the work to the operating system kernel, and when the IO operation supplies data (as an event), it will notify your code with your supplied callbacks.

  • 2

    Basics

    require('http').globalAgent.maxSockets = 25
    
    // You can change 25 to Infinity or to a different value by experimenting
    

    Node.js by default is using maxSockets = Infinity at the same time (since v0.12.0). Until Node v0.12.0, the default was maxSockets = 5 (see v0.11.0). So, after more than 5 requests they will be queued. If you want concurrency, increase this number.

    Setting your own agent

    http API is using a "Global Agent". You can supply your own agent. Like this:

    const http = require('http')
    const myGloriousAgent = new http.Agent({ keepAlive: true })
    myGloriousAgent.maxSockets = Infinity
    
    http.request({ ..., agent: myGloriousAgent }, ...)
    

    Turning off Socket Pooling entirely

    const http = require('http')
    const options = {.....}
    
    options.agent = false
    
    const request = http.request(options)
    

    Pitfalls

    • You should do the same thing for https API if you want the same effects

    • Beware that, for example, AWS will use 50 instead of Infinity.

Please consider making a request to improve this example.

Syntax

Syntax

Parameters

Parameters

Remarks

Remarks

Still have a question about Node.js Performance? Ask Question

Topic Outline


    We are no longer accepting contributions to Documentation. Drafts cannot be modified.