Skip to content

Commit

Permalink
Use performance-now instead of custom solution
Browse files Browse the repository at this point in the history
  • Loading branch information
nicjansma committed Feb 17, 2017
1 parent a9ad38a commit 3f57975
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 28 deletions.
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -38,6 +38,7 @@
"mime-types": "~2.1.7",
"node-uuid": "~1.4.7",
"oauth-sign": "~0.8.1",
"performance-now": "^0.2.0",
"qs": "~6.3.0",
"stringstream": "~0.0.4",
"tough-cookie": "~2.3.0",
Expand Down
34 changes: 6 additions & 28 deletions request.js
Expand Up @@ -28,6 +28,7 @@ var http = require('http')
, Multipart = require('./lib/multipart').Multipart
, Redirect = require('./lib/redirect').Redirect
, Tunnel = require('./lib/tunnel').Tunnel
, now = require('performance-now')

var safeStringify = helpers.safeStringify
, isReadStream = helpers.isReadStream
Expand All @@ -36,7 +37,6 @@ var safeStringify = helpers.safeStringify
, copy = helpers.copy
, version = helpers.version
, globalCookieJar = cookies.jar()
, hrTimeStart


var globalPool = {}
Expand Down Expand Up @@ -92,28 +92,6 @@ function responseToJSON() {
}
}

function getHrTime() {
if (typeof process === 'undefined' || !process.hrtime) {
return 0
}

var hr = process.hrtime()
// convert to nanoseconds
return hr[0] * 1e9 + hr[1]
}

hrTimeStart = getHrTime()

function getTimeFromStart() {
// in the browser, use performance.now()
if (typeof performance !== 'undefined' && performance.now) {
return performance.now()
}

// in nodejs, use process.hrtime() (converting back to milliseconds)
return (getHrTime() - hrTimeStart) / 1e6
}

function Request (options) {
// if given the method property in options, set property explicitMethod to true

Expand Down Expand Up @@ -739,7 +717,7 @@ Request.prototype.start = function () {
var self = this

if (self.timing) {
var startTime = getTimeFromStart()
var startTime = now()
}

if (self._aborted) {
Expand Down Expand Up @@ -799,9 +777,9 @@ Request.prototype.start = function () {
})
self.req.on('socket', function(socket) {
if (self.timing) {
self.timings.socket = getTimeFromStart()
self.timings.socket = now()
socket.on('connect', function() {
self.timings.connect = getTimeFromStart()
self.timings.connect = now()
})
}

Expand Down Expand Up @@ -888,13 +866,13 @@ Request.prototype.onRequestResponse = function (response) {
var self = this

if (self.timing) {
self.timings.response = getTimeFromStart()
self.timings.response = now()
}

debug('onRequestResponse', self.uri.href, response.statusCode, response.headers)
response.on('end', function() {
if (self.timing) {
self.timings.end = getTimeFromStart()
self.timings.end = now()

self.timings.dns = self.timings.socket - self.timings.start
self.timings.tcp = self.timings.connect - self.timings.socket
Expand Down

0 comments on commit 3f57975

Please sign in to comment.