Skip to content

Instantly share code, notes, and snippets.

@mousavian
Forked from natesilva/axios-vs-superagent.js
Last active January 23, 2018 06:59
Show Gist options
  • Save mousavian/e22af68630d95bd4b602681b009efc00 to your computer and use it in GitHub Desktop.
Save mousavian/e22af68630d95bd4b602681b009efc00 to your computer and use it in GitHub Desktop.
Compare performance of Axios vs. SuperAgent when running under Node.js
const HttpAgent = require('agentkeepalive')
const superagent = require('superagent')
const Benchmark = require('benchmark')
const axios = require('axios')
const http = require('http')
var suite = new Benchmark.Suite()
const targetUrl = 'http://httpbin.org/ip'
const axiosInstance = axios.create({
baseURL: 'http://httpbin.org',
httpAgent: http.httpAgent
})
const httpKeepAliveAgent = new HttpAgent({
keepAlive: true,
keepAliveMsecs: 2000,
keepAliveTimeout: 30000,
timeout: 60000
})
suite
.add('axios', {
defer: true,
fn: function (deferred) {
axios.get(targetUrl).then(function () { deferred.resolve() })
}
})
.add('axios(keepalive)', {
defer: true,
fn: function (deferred) {
axiosInstance.get('/ip').then(function () { deferred.resolve() })
}
})
.add('superagent(promise)', {
defer: true,
fn: function (deferred) {
superagent.get(targetUrl).then(function () { deferred.resolve() })
}
})
.add('superagent(end method)', {
defer: true,
fn: function (deferred) {
superagent.get(targetUrl).end(function () { deferred.resolve() })
}
})
.add('superagent(end+agent)', {
defer: true,
fn: function (deferred) {
superagent.agent(httpKeepAliveAgent).get(targetUrl).end(function () { deferred.resolve() })
}
})
.on('cycle', function (event) { console.log(String(event.target)) })
.on('complete', function () {
console.log('Fastest is ' + this.filter('fastest').map('name'))
})
.run({ async: true })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment